我正在学习等待node.js
的承诺 以下程序通过调用底部的main()
来运行。main()
调用函数caller()
。 然后caller()
调用函数loop()
,超时为 2 秒并返回我等待了 2 秒的消息。loop()
返回caller()
,然后caller()
返回main()
,只是为了传递相同的消息。
caller() 和 main() 实际上是相同的代码,它们应该以相同的方式返回! 但是返回 main() 是不确定的。
日食运行结果:
C:\Users\Lenovo\eclipse-workspace.metadata.plugins\org.eclipse.wildwebdeveloper.embedder.nodeode-v14.15.4-win-x64ode.exe awaitPromise2.js "顶级开始。主要开始...在循环中,启动 aaa :新循环,完成输出:消息:我在呼叫者中等待了 1 秒 aaa : 消息:我在呼叫者中等待了 1 秒,解决 aaa: 消息: I 在主中等待 1 秒,响应:主中未定义,解决结果: 定义
程序文件为:
async function loop() {
let aaa = "New";
console.log("in loop, start aaa : " + aaa);
await new Promise((resolve, reject) => setTimeout(resolve, 2000));
aaa = " Message: I waited 2 sec.";
return new Promise((resolve) => {
console.log("loop, finished output : " + aaa);
resolve(aaa);
return;
});
}
async function caller() {
await loop().then(function (aaa) {
new Promise((resolve, reject) => setTimeout(resolve, 3000));
console.log("in caller aaa :" + aaa);
return new Promise((resolve) => {
new Promise((resolve, reject) => setTimeout(resolve, 1500));
let bbb = aaa;
console.log("in caller, resolve aaa : " + bbb);
resolve(bbb);
});
});
}
async function main() {
console.log("main start ...");
await caller().then(function (aaa) {
new Promise((resolve, reject) => setTimeout(resolve, 5000));
console.log("in main, response :" + aaa);
return new Promise((resolve) => {
new Promise((resolve, reject) => setTimeout(resolve, 6000));
console.log("in main, resolve result : " + aaa);
resolve(aaa);
return;
});
});
}
console.log(" Top Level starts..");
main();
我利用超时试图等待结果的到来。需要他们吗??
让我们从代码中删除所有多余的东西,这些东西什么也没做:
async function loop() {
let aaa = "New";
console.log("in loop, start aaa : " + aaa);
await new Promise((resolve, reject) => setTimeout(resolve, 2000));
aaa = " Message: I waited 2 sec.";
console.log("loop, finished output : " + aaa);
return Promise.resolve(aaa);
}
async function caller() {
await loop().then(function (aaa) {
console.log("in caller aaa :" + aaa);
let bbb = aaa;
console.log("in caller, resolve aaa : " + bbb);
return Promise.resolve(bbb);
});
}
async function main() {
console.log("main start ...");
await caller().then(function (aaa) {
console.log("in main, response :" + aaa);
console.log("in main, resolve result : " + aaa);
return Promise.resolve(aaa);
});
}
console.log(" Top Level starts..");
main();
现在问题很清楚了:caller
不return
一个值。它包含的唯一return
语句是在.then()
回调中,解析await
ed 的承诺,但之后没有任何反应。要解决这个问题,return
承诺链,而不仅仅是await
它:
function caller() {
return loop().then(function (aaa) {
console.log("in caller, aaa: " + aaa);
return Promise.resolve(aaa);
});
}
或者最好不要使用.then()
链接,因为您可以在任何地方使用async
/await
。您也可以在async
函数或.then
回调中省略返回值中的Promise.resolve
:
async function loop() {
const aaa = "New";
console.log("in loop, start: " + aaa);
await new Promise((resolve, reject) => setTimeout(resolve, 2000));
const bbb = "Message: I waited 2 sec.";
console.log("loop, finished output: " + bbb);
return bbb;
}
async function caller() {
const bbb = await loop();
console.log("in caller:" + bbb);
return bbb;
}
async function main() {
console.log("main start ...");
const res = await caller();
console.log("in main, response: " + res);
// return res; // unused anyway
}
console.log("Top Level starts...");
main();
伙计,你刚刚错过了呼叫者中的一个return
。或者,您错误地使用了await
。因为您使用了await
所以除非调用 return,否则不会返回结果。
async function loop() {
let aaa = "New";
console.log("in loop, start aaa : " + aaa);
await new Promise((resolve, reject) => setTimeout(resolve, 2000));
aaa = " Message: I waited 2 sec.";
return new Promise((resolve) => {
console.log("loop, finished output : " + aaa);
resolve(aaa);
return;
});
}
async function caller() {
return await loop().then(function (aaa) {
new Promise((resolve, reject) => setTimeout(resolve, 3000));
console.log("in caller aaa :" + aaa);
return new Promise((resolve) => {
new Promise((resolve, reject) => setTimeout(resolve, 1500));
let bbb = aaa;
console.log("in caller, resolve aaa : " + bbb);
resolve(bbb);
});
});
}
async function main() {
console.log("main start ...");
await caller().then(function (aaa) {
new Promise((resolve, reject) => setTimeout(resolve, 5000));
console.log("in main, response :" + aaa);
return new Promise((resolve) => {
new Promise((resolve, reject) => setTimeout(resolve, 6000));
console.log("in main, resolve result : " + aaa);
resolve(aaa);
return;
});
});
}
console.log(" Top Level starts..");
main();
我建议你不要把async
和.then
混为一谈,你应该选择一个并坚持下去。
我建议你放弃caller()
async/await
,这更容易理解:
async function loop() {
let aaa = "New";
console.log("in loop, start aaa : " + aaa);
await new Promise((resolve, reject) => setTimeout(resolve, 2000));
aaa = " Message: I waited 2 sec.";
return new Promise((resolve) => {
console.log("loop, finished output : " + aaa);
resolve(aaa);
return;
});
}
function caller() {
return loop().then(function (aaa) {
new Promise((resolve, reject) => setTimeout(resolve, 3000));
console.log("in caller aaa :" + aaa);
return new Promise((resolve) => {
new Promise((resolve, reject) => setTimeout(resolve, 1500));
let bbb = aaa;
console.log("in caller, resolve aaa : " + bbb);
resolve(bbb);
});
});
}
async function main() {
console.log("main start ...");
await caller().then(function (aaa) {
new Promise((resolve, reject) => setTimeout(resolve, 5000));
console.log("in main, response :" + aaa);
return new Promise((resolve) => {
new Promise((resolve, reject) => setTimeout(resolve, 6000));
console.log("in main, resolve result : " + aaa);
resolve(aaa);
return;
});
});
}
console.log(" Top Level starts..");
main();
如果你是一个新手,我挑战你尝试重写你的代码,不要完全使用.then
,然后反过来 - 根本不使用async/await
。