你不需要那种全球性的
temp
变量和
setTimeout
second
真的不管用。应该是这样的
cb("I am second");
,就像你通常打电话的地方一样
resolve("I am second");
在一个
new Promise
. 然后,您可以将该值作为要传递给的回调函数的参数接收
second(â¦)
const first = () => {
return "I am first";
}
const second = (cb) => {
setTimeout(function() {
cb("I am second");
}, 1000);
}
const third = () => {
return "I am third";
}
const solve = () => {
var f = first();
console.log(f);
second((s) => {
console.log(s);
const t = third();
console.log(t);
});
}
solve();
注意,如果要使用,这与您的承诺版本没有什么不同
.then()
而不是
async
await
语法:
const solve = () => {
var f = first();
console.log(f);
second().then((s) => {
console.log(s);
var t = third();
console.log(t);
});
}