Ama*_*yaz 0 asynchronous node.js express node-modules nodejs-stream
setTimeout(()=>{
console.log('time out')
},3000)
}
go();
console.log('app')
Run Code Online (Sandbox Code Playgroud)
这是异步代码,我想在延迟后打印应用程序,但是我们知道先打印“应用程序”,然后再打印“超时”。
您可以通过两种方式处理异步任务:
第一种方式:
function promiseFunction() {
return new Promise((resolve, reject) => {
setTimeout(()=>{
console.log('completed task and resolve');
resolve()
},3000)
})
}
promiseFunction().then(() => {
console.log('all task completed with your message (app)');
})
Run Code Online (Sandbox Code Playgroud)
第二种方式:-
asyncFunction();
function promiseFunction() {
return new Promise((resolve, reject) => {
setTimeout(()=>{
console.log('completed task and resolve');
resolve()
},3000)
})
}
async function asyncFunction() {
await promiseFunction();
console.log('all task completed with your message (app)');
}
Run Code Online (Sandbox Code Playgroud)
PS请确保您的await关键字应该在异步功能中。