简体   繁体   English

JavaScript承诺在我的情况下不起作用

[英]Javascript promises not working in my case

Its a simple case 这是一个简单的案例

var q = new Promise(function(resolve, reject){
        resolve(initRequest('get', 'includes/getRemessaRegistrada.php', null, remessasList));
    }).then(function(){
        console.log('ok');
    });

I'm learning javascript promises concept. 我正在学习javascript promises概念。 Why console.log('ok') is coming before resolve processing? 为什么在解析处理之前会出现console.log('ok')?

In your code initRequest() does not return anything. 在您的代码中, initRequest()不返回任何内容。 For your code to work as you have it, initRequest() must return a promise that is tied to the underlying asynchronous operation. 为了使您的代码按原样工作, initRequest()必须返回与底层异步操作相关的promise。 And, if it did return a promise, then you don't need another wrapping promise around that. 而且,如果它确实返回了一个承诺,那么您不需要围绕它的另一个包装承诺。

Promises by themselves do not have magical powers to somehow know when an underlying asynchronous operation completes. 承诺本身没有神奇的力量,无法以某种方式知道底层异步操作何时完成。 They just don't have such powers. 他们只是没有这种权力。 A promise only works when there's an asynchronous operation that calls resolve or reject on the promise at the appropriate time when the asynchronous operation completes with a value or an error. 当异步操作以值或错误结束时,在适当的时候有一个异步操作调用对promise的拒绝或拒绝,promise才有效。 The promise then serves as an easier to use interface for managing and coordinating asynchronous results or errors. 然后,promise成为易于使用的界面,用于管理和协调异步结果或错误。

You are running initRequest() which starts your async operation and then immediately returns, then long before it finishes, you are calling resolve(undefined) for your wrapping promise. 您正在运行initRequest() ,该操作将启动异步操作,然后立即返回,然后在完成之前很长一段时间,您正在为您的包装承诺调用resolve(undefined) This promise will resolve long before initRequest() is done and will not know anything about the async return value. 这个承诺将在initRequest()完成之前解决,并且不会对异步返回值有任何了解。

The best solution here would be to get or use a promisified version of XMLHttpRequest so you can make ajax calls with functions that just return promises. 最好的解决方案是获取或使用XMLHttpRequest的承诺版本,以便您可以使用仅返回Promise的函数进行Ajax调用。 jQuery has one built in. The latest browsers have fetch() which does Ajax and returns a promise and there are dozens of small libraries available to do this or you could make your own. jQuery具有内置功能。最新的浏览器具有fetch()函数,该函数执行Ajax并返回Promise,并且有数十种小型库可用于执行此操作,或者您可以自己创建。 If you aren't already using a library that has this built in (like jQuery) and you need to get something specifically for this, I'd probably get a fetch() polyfill since this is clearly the future direction for browsers. 如果您尚未使用内置此功能的库(如jQuery),并且需要专门为此做一些准备,那么我可能会得到fetch() polyfill,因为这显然是浏览器的未来方向。

If you wanted just a simple XHR wrapper or a way to make ajax calls using a promise interface, here are some ways to go: 如果您只需要一个简单的XHR包装器或使用Promise接口进行Ajax调用的方法,则可以采用以下几种方法:

How do I promisify native XHR? 我如何保证本地XHR?

XR XR

Fetch Polyfill 获取Polyfill


It also occurs to me that because you have return req.responseText in your initRequest() function, you may think that initRequest() is returning that value. 我还想到由于在initRequest()函数中return req.responseText ,您可能会认为initRequest()正在返回该值。 It is not. 它不是。 initRequest() returns nothing. initRequest()返回任何内容。 return req.responseText is being returned to an async callback which just goes into the bowels of the XMLHttpRequest implementation. return req.responseText返回到异步回调,该回调仅进入XMLHttpRequest实现的内容。 Your initRequest() function has long, long since already returned and returned undefined . 您的initRequest()函数已经存在很长时间了,并且已经返回undefined For more on why this is the case, see How do I return the response from an asynchronous call? 有关这种情况的更多信息,请参见如何返回异步调用的响应? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM