帶 async 的函數(async function fn(){}),會返回 Promise 函數javascript
await 至關於 promise 構造函數中的 resolve 參數java
async function fn(){
code1...;
const value=await fn2();
code2...;
}
複製代碼
類等於es6
new Promise(resolve=>{
code1...;
resolve(fn2());
}).then(value=>{
code2...;
})
複製代碼
function syncFun() {
console.log('syncFun start')
return 'value'
}
async function asyncFun() {
console.log('async start')
const value = syncFun()
console.log('value=' + value)
console.log('async end')
}
asyncFun()
console.log('main end')
複製代碼
function syncFun() {
console.log('syncFun start')
return 'value'
}
new Promise(resolve => {
console.log('promise start')
const value = syncFun()
console.log('value=' + value)
console.log('promise end')
})
console.log('main end')
複製代碼
function syncFun() {
console.log('syncFun start')
return 'value'
}
async function asyncFun() {
console.log('async start')
const value = await syncFun()
console.log('value=' + value)
console.log('async end')
}
asyncFun()
console.log('main end')
複製代碼
function syncFun() {
console.log('syncFun start')
return 'value'
}
new Promise(resolve => {
console.log('promise start')
resolve(syncFun())
}).then(value => {
console.log('value=' + value)
console.log('promise end')
})
console.log('main end')
複製代碼
async function syncFun() {
console.log('syncFun start')
return 'value'
}
async function asyncFun() {
console.log('async start')
const value = await syncFun()
console.log('value=' + value)
console.log('async end')
}
asyncFun()
console.log('main end')
複製代碼
async function syncFun() {
console.log('syncFun start')
return 'value'
}
new Promise(resolve => {
console.log('promise start')
resolve(syncFun())
}).then(value => {
console.log('value=' + value)
console.log('promise end')
})
console.log('main end')
複製代碼
沒看過 async/await 的源碼,只是根據使用的時候的直覺而寫,若有不妥之處,歡迎指正(歡迎加微信 772345373)segmentfault