简体   繁体   English

在Promise.prototype.then中将promise作为onFulfilled函数传递

[英]Passing promises as onFulfilled function in Promise.prototype.then

I recently discovered that you can pass a promise, instead of a function as onFulfilled function in promise.then 我最近发现,您可以传递一个onFulfilled ,而不是使用onFulfilled函数promise.then

For example: 例如:

 var p = new Promise(function(resolve, reject) { setTimeout(function(){ resolve("First Promise"); }, 4000); }); var q = new Promise(function(resolve, reject) { setTimeout(function(){ console.log("Yo"); resolve("Second Promise"); }, 8000); }); p.then(q).then(function(data){ console.log(data); }); 

This will print the following output 这将打印以下输出

First Promise (at 4th second)
Yo (at 8th second)

This sounded weird to me, because as practise, we only pass functions in promise.then , instead of passing a new Promise. 这听起来很奇怪,因为实际上,我们只在promise.then传递函数,而不是传递新的Promise。

Can someone help me certain use case, where it can be used? 有人可以帮助我某些用例吗? And, what is the exact behaviour, if you pass promise inside promise.then ? 而且,如果您在promise.then内通过promise,那么确切的行为是什么?

EDIT: 编辑:

I saw similar behaviour on Spotify Web Player. 我在Spotify Web Player上看到了类似的行为。 在此处输入图片说明

this._onStreamerConnect() returns a Promise, which has .then attached on it. this._onStreamerConnect()返回一个无极,这已经.then安装就可以了。 .then takes a parameter e which is also a Promise, as seen from the Watch tab in the right panel .then采用参数e ,它也是一个Promise,如右侧面板中的“监视”选项卡所示

According to MDN documentation : 根据MDN文档

If one or both arguments are omitted or are provided non-functions, then then will be missing the handler(s), but will not generate any errors. 如果一个或两个参数被忽略或提供非功能,然后then将缺少处理程序(一个或多个),但不会产生任何错误。 If the Promise that then is called on adopts a state ( fulfillment or rejection ) for which then has no handler, a new Promise is created with no additional handlers, simply adopting the final state of the original Promise on which then was called. 如果then调用的Promise采用了没有处理程序的状态( fulfillmentrejection ), then将创建一个没有其他处理程序的新Promise ,只需采用then被调用的原始Promise的最终状态即可。

This means that calling p.then(q).then(function...) is the same as calling p.then(function...) . 这意味着调用p.then(q).then(function...) p.then(function...)与调用p.then(function...) The q parameter is completely ignored and not part of the promise chain. q参数将被完全忽略,而不是promise链的一部分。

q 's executor function eventually resolves in 8 seconds, independent of p , which still resolves in 4 seconds, and nothing consumes the string "Second Promise" as you can see from the output. q的执行程序函数最终在8秒内解析,而与p无关, p仍在4秒内解析,并且从输出中可以看到,没有任何东西消耗字符串"Second Promise"

To add to Patrick Roberts's correct answer , you can return a Promise from then , and the then function will wait for the Promise before continuing . 要添加帕特里克·罗伯茨(Patrick Roberts)的正确答案 ,您可以then 返回Promisethen then函数将等待Promise之后再继续 However, to do that, you need to pass a function to then that returns a Promise . 但是,要做到这一点,您需要传递一个函数, then返回Promise

As such, if your line were to look like this: 因此,如果您的行看起来像这样:

p.then(() => q).then(function(data){
    console.log(data);
});

Or without lambdas: 或不带lambda:

p.then(function() { return q; }).then(function(data){
    console.log(data);
});

Then you would see the output: 然后,您将看到输出:

First Promise (at 4th second)
Yo (at 8th second)
Second Promise (at 8th second)

EDIT : To your point about Spotify, most calls to the _runOnDevice function you're looking at have explicitly-bound functions as their second and third arguments. 编辑 :关于Spotify,对您正在查看的_runOnDevice函数的大多数调用都将显式绑定的函数作为其第二和第三个参数。

Most of the argument names are the same ( t , e , n ) despite their positions, which makes it a bit confusing to follow. 尽管参数名称的位置相同,但大多数参数名称都是相同的( ten ),这使得后面的用法有些混乱。 I'd guess that these are transpiled async functions heavily using lambdas. 我猜想这些都是大量使用lambdas进行转译的async函数。

As Patrick pointed out in the comments, at least one call to _runOnDevice appears to take a Promise as a second parameter. 正如Patrick在评论中指出的那样,至少对_runOnDevice一次调用似乎将Promise作为第二个参数。 That looks like a bug to me. 在我看来,这似乎是个虫子。

Spotify代码:vendor.125e9736.js

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

相关问题 在Promise.prototype.then()中传递参数 - Passing argument in Promise.prototype.then() Promise.prototype.then内的Javascript函数不执行 - Javascript function inside Promise.prototype.then not executing 为什么 Promise.prototype.then 总是返回承诺? - Why does Promise.prototype.then always return promises? 更好地了解Promise.prototype.then - A Better understanding on Promise.prototype.then 如何在Promise.prototype.then()中包含条件 - How to include a conditional in Promise.prototype.then() 为什么 Promise.prototype.then 允许跳过拒绝 function 并在末尾允许逗号? - Why does Promise.prototype.then allow skipping reject function and allow comma at the end? 无法理解 MDN 文档中带有 Promise.prototype.then() 的注释 - Could not understand the note with Promise.prototype.then() in MDN Document TypeError:方法 Promise.prototype.then 在不兼容的接收器代理上调用 - TypeError: Method Promise.prototype.then called on incompatible receiver Proxy Promise.prototype.then 链在以下方面会发生什么:调用堆栈、WebAPI 部分和微任务队列? - What happens with a Promise.prototype.then chain with regards to the: call-stack, WebAPI section and micro-task queue? 无法覆盖Promise类上的方法 - Promise.prototype.then调用不兼容的接收器undefined - Unable to Override methods on Promise class - Promise.prototype.then called on incompatible receiver undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM