async and await handle asynchrony gracefully

Article Directory

function delay(word) {
    
    
    return new Promise((resolve, reject) => {
    
    
        setTimeout(()=>{
    
    
            resolve('hello ' + word)
        }, 2000)
    })
}

async function start(){
    
    
    const word1 = await delay('孙悟空')
    console.log(word1)
    const word2= await delay('猪八戒')
    console.log(word2)
    const word3 = await delay('沙悟净')
    console.log(word3)
}
start()

Results of the:
Insert picture description here


There is no nesting hell, and there are no multiple parentheses and then promise calls. You can use variables to receive asynchronous return values, and then write like synchronous code, great! !

Guess you like

Origin blog.csdn.net/baidu_21349635/article/details/108168829