setTimeout(()=>{
console.log('time out')
},3000)
}
go();
console.log('app')
这是异步代码,我想打印应用程序后的延迟,但正如我们所知,“应用程序”是先打印,然后“超时”。
发布于 2019-11-05 02:41:42
您可以通过两种方式处理异步任务:-
有承诺的
的方法
第一条路:-
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)');
})
第二条路:-
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)');
}
请确保您的等待关键字应该处于异步功能中。
发布于 2019-11-05 02:20:29
您可以使用允诺处理异步代码。
function go() {
return new Promise((resolve, reject) => {
setTimeout(()=>{
console.log('time out');
resolve()
},3000)
})
}
go().then(() => {
console.log('app')
})
https://stackoverflow.com/questions/58709143
复制相似问题