本文介绍了"等候"在使用本机(ES6)JavaScript承诺的承诺或常规对象上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前,我使用jQuery或Q进行了几次诺言。我现在很生锈,想要学习和使用新的本机ES6承诺。



我似乎记得一个整洁的概念,你可以等待一些东西,而不在乎它是一个普通的对象或承诺。如果它是一个承诺,回调在异步完成时被调用,如果还有其他的回调被立即调用 - 也许下一个勾选。



但我不记得如何这个做完了。我不知道它是否有一个名字,所以Google证明很难。我不知道它是否是所有JS承诺实现中的标准功能,或者只是jQuery有的东西。



这是什么叫?我可以用本地的承诺来做这个吗?在哪里可以阅读?

解决方案

jQuery的 $。when()和ES6的 Promise.all()表现出您引用的行为。提供承诺和功能等待承诺解决,但任何其他价值,它立即返回。


I played with promises a few times a few years ago using either jQuery or Q. I'm now quite rusty and want to learn and use the new native ES6 promises.

I seem to remember one neat concept where you can "wait" on something and not care whether it's a plain object or a promise. If it's a promise the callback is called when it asynchronously completes, if it's anything else the callback is called immediately - maybe the next tick.

But I can't recall how this is done. I'm not sure if it has a name so it's proving difficult to Google for. I'm not sure if it's a standard feature across all JS promise implementations, or if it was just something only jQuery had.

What is this called? Can I still do this with native promises? Where can I read up on it?

解决方案

Both jQuery's $.when() and ES6's Promise.all() exhibit the behaviour you refer to. Provide a promise and the function waits for the promise to resolve, but for any other value it returns immediately.

这篇关于"等候"在使用本机(ES6)JavaScript承诺的承诺或常规对象上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:46