[英]How to chain promises
In this simplified example, I am calling three functions when the first promise gets resolved. 在这个简化的示例中,当第一个承诺得到解决时,我将调用三个函数。
var test = new Promise(function (res, err) { setTimeout(res, 3000) })
test.then( () => { console.log("A") });
test.then( () => {
return new Promise(function(res, err) {
setTimeout(()=> { console.log("C");
res() }, 3000)
});
});
test.then( () => { console.log("B") });
The output as expected is ABC
. 预期的输出是ABC
。
Let's suppose that I want to invoke the third .then
to console B
only when the second promise gets resolved. 假设我想仅在第二个承诺得到解决时才调用第三个.then
来控制台B
If I try to store the second promise ( myProm
) in global and attach a .then
function on it, I'll get (reasonably) a TypeError because at compile time myProm is still undefined. 如果我尝试存储第二承诺( myProm
全球),并附上.then
在其上的功能,我会得到(合理的)一个类型错误,因为在编译时myProm仍然是不确定的。
var test = new Promise(function (res, err) { setTimeout(res, 3000) })
var myProm;
test.then( () => { console.log("A") });
test.then( () => {
myProm = new Promise(function(res, err) {
setTimeout(()=> { console.log("C");
res() }, 3000)
})
return myProm;
});
myProm.then( () => { console.log("B") });
How do I proceed? 我该如何进行? What's the best way to chain two promises together so that the returned Promise obj from one .then
needs to get resolved before we can execute the next then
. 什么是连锁的最佳方式二的承诺一起,这样从一个返回的承诺OBJ .then
需要得到解决之前,我们可以执行下一次then
。
In this scenario, the output I'd like to have would be ACB
在这种情况下,我想要的输出将是ACB
then
returns a promise that resolves when the indicated function has run and the value it returned has resolved (this is a slight oversimplification, but sufficient for the task here). then
返回一个promise,当指定的函数运行并且返回的值已解析时,promise会解析(这有点过分简化,但足以完成此处的任务)。
Therefore, to chain a promise on the result of a then
. 因此,要在then
的结果上链接一个承诺。 Just tack on another .then
: 只是钉在另一个.then
:
var test = new Promise(function (res, err) { setTimeout(res, 3000) }) test.then( () => { console.log("A") }); test .then( () => { return new Promise(function(res, err) { setTimeout(()=> { console.log("C"); res() }, 3000); }); }) .then( () => { console.log("B") });
Each time you call .then
, you create a new Promise
that resolves when the Promise
returned by that .then
resolves. 每次你打电话.then
,您创建一个新的 Promise
解析时Promise
返回通过.then
解决。 You should assign the result of the myProm
-containing .then
to a variable, and then call .then
on that variable: 您应该分配的结果myProm
含.then
给一个变量,然后调用.then
该变量:
var test = new Promise(function (res, err) { setTimeout(res, 500) }) var myProm; test.then( () => { console.log("A") }) .then( () => { myProm = new Promise(function(res, err) { setTimeout(()=> { console.log("C"); res() }, 500) }) return myProm; }) .then( () => { console.log("B") });
Most of the time when using Promises
, you should be chaining .then
s like this. 在大多数情况下使用时Promises
,你应该链接.then
这样的。 Only do prom.then(...) ... prom.then(...)
when you want to initialize two completely separate asynchronous operations when the prom
resolves. 仅当要在prom
解析时初始化两个完全独立的异步操作时才执行prom.then(...) ... prom.then(...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.