Here, I'm assuming function2
is asynchronous and performs some kind of async work. I'm simulating that async work in the code below with the delay
function.
In your code, you do not call function2
; the ()
is missing from the function call.
Also, the call is not await
ed. Since function2
is async, it will begin running but execution will continue on as await
is required to tell JS you want to wait until the function's returned promise is resolved before continuing.
If this is still unclear, I advise you read the MDN docs on async/await
The following is a working example:
const function2 = async () => {
try {
// .. do some async stuff, such as:
const delay = ms => new Promise(res => setTimeout(res, ms))
await delay(3000)
return true;
}
catch {
return false;
}
}
const function1 = async () => {
let isSuccess = false;
isSuccess = await function2();
return isSuccess;
}
// Test it out:
async function main(){
console.log("calling function1...");
console.log("function1 returned: " + await function1());
};
main();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…