假設我有一個foo()回傳promise. 我將如何“啟動”async函式foo(),在決議時執行一些代碼foo(),然后在決議后繼續執行其余代碼foo()?
// start async function foo
const somePromise = foo(); // do not use await here in order to execute the following lines
// synchronous code
const a = ....;
const b = ....;
// and then, go on with the rest of the code after the synchronous (const a = ..., const b = ...) and asynchronous (foo()) parts have finished
if(somePromise resolved) { // how to check for resolved promise here
// go on
const bar = a b;
}
uj5u.com熱心網友回復:
很有趣,您甚至then在問題中使用了這個詞,但是:
const somePromise = foo();
// do something synchronous while foo() is resolving
// and then, go on with the rest of the code after the synchronous and asynchronous parts have finished
somePromise.then(() => {
// go on
});
你最好立即處理它;
foo().then(() => {
// go on
});
// This happens synchronously
第一個的變體,具有await:
const somePromise = foo();
// do something synchronous while foo() is resolving
// and then, go on with the rest of the code after the synchronous and asynchronous parts have finished
await somePromise;
// go on
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/523576.html
上一篇:使用嵌套回圈作為流
