[英]JavaScript Promises and setTimeout
I have been playing with Promises, but I am having trouble understanding what is happening with the following code: 我一直在玩Promises,但我无法通过以下代码了解正在发生的事情:
function a() {
return new Promise(function (resolve, reject) {
resolve("hi from a!");
});
}
function b() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve("hi from b!");
}, 5000);
});
}
function c() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve("hi from c!");
}, 1000);
});
}
a().then(function (resultFromA) {
console.log("a() responded with: " + resultFromA);
b();
}).then(function (resultFromB) {
console.log("b() responded with: " + resultFromB);
c();
}).then(function (resultFromC) {
console.log("c() responded with: " + resultFromC);
});
I am expecting this to output a() responded with: hi from a!
我期待这个输出
a() responded with: hi from a!
immediately, along with b() responded with: hi from b!
马上,与
b() responded with: hi from b!
and c() responded with: hi from c!
并且
c() responded with: hi from c!
after their respective setTimeout()
fires. 在各自的
setTimeout()
触发后。 However, what I get this the following output immediately: 但是,我立即获得以下输出:
a() responded with: hi from a!
a()回复:嗨来自!
b() responded with: undefined
b()回复:undefined
c() responded with: undefined
c()回复:undefined
I was thinking that .then()
waits on these promises, but it isn't. 我在想
.then()
会等待这些承诺,但事实并非如此。 Any help would be appreciated. 任何帮助,将不胜感激。
You need to return b()
and return c()
from within your then
handlers. 您需要
return b()
并从then
处理程序中return c()
。
then
only "replaces" the first promise with a subsequent promise which is returned from its callback. then
只用从其回调返回的后续承诺“替换”第一个承诺。
If your then
callback doesn't return
a promise, then the then
applies to the original promise, and it will be executed immediately regardless of the contents/result of the previous then
callback. 如果你
then
回调不return
一个承诺,然后then
应用到原来的承诺,将立即不管前面的内容/结果的执行then
回调。
Basically... 基本上...
a().then(function () {
b()
}).then( # This "then" is chained off of a's promise
While conversely: 反之亦然:
a().then(function () {
return b()
}).then( # This "then" is chained off of b's promise
You need to return promises
to chain them : 你需要
return promises
链接它们:
a().then(function (resultFromA) {
console.log("a() responded with: " + resultFromA);
// return b() promise
return b();
}).then(function (resultFromB) {
console.log("b() responded with: " + resultFromB);
// return c() promise
return c();
}).then(function (resultFromC) {
console.log("c() responded with: " + resultFromC);
});
You forgot to return from function call. 你忘了从函数调用返回。 Javascript function does not return implicitly.
Javascript函数不会隐式返回。
function a() {
return new Promise(function (resolve, reject) {
resolve("hi from a!");
});
}
function b() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve("hi from b!");
}, 5000);
});
}
function c() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve("hi from c!");
}, 1000);
});
}
a().then(function (resultFromA) {
console.log("a() responded with: " + resultFromA);
return b(); // return
}).then(function (resultFromB) {
console.log("b() responded with: " + resultFromB);
return c(); // return
}).then(function (resultFromC) {
console.log("c() responded with: " + resultFromC);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.