1 async 函数执行结果都是Promise
async function HiAsync() {
return "hi";
}
async function HelloAsync() {
return Promise.resolve('hello')
}
console.log(HiAsync())
console.log(HelloAsync())
HiAsync().then(r => {
console.log(r) // 'hi'
})
HelloAsync().then(r => {
console.log(r) // 'hello'
})
2 await 总能等到结果
function getSomething() {
return "a";
}
async function testAsync() {
return new Promise((re, rj) => {
setTimeout(() => { re('b') }, 500)
})
}
async function deepAsync() {
let p2 = new Promise((re, rj) => {
setTimeout(() => { re('c') }, 10)
})
return new Promise((re, rj) => {
setTimeout(() => { re(p2) }, 500)
})
}
async function test() { // await 的使用时 必须在async 函数中
const v1 = await getSomething();
console.log('v1',v1)
const v2 = await testAsync();
console.log('v2',v2)
const v3 = await deepAsync();
console.log('v3',v3);
}
test();
3 await 的使用时 必须在async 函数中