async function async1 () {
console.log('async1 start')
await new Promise(resolve => {
setTimeout(() => {
console.log('async2')
resolve();
}, 0)
})
console.log('async1 end')
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout')
}, 0)
async1();
new Promise (function (resolve) {
setTimeout(function () {
console.log('promise1')
}, 0)
resolve();
}).then (function () {
console.log('promise2')
})
console.log('script end')
上面打印结果如下:
script start
async1 start
script end
promise2
setTimeout
async2
async1 end
promise1