async and await ---- js asynchronous processing

async and await ---- js asynchronous processing

Blog Description

Information in the article involved from the Internet and organize personal conclusion, meaning that the individual learning experience and summary, what if infringement, please contact me to delete, thank you!

Explanation

Before writing code encounters a problem, the return value is a promise object before consequently do not understand, and now understand a little what, to learn about the record.

effect

async is "asynchronous" shorthand, and await can be considered async wait shorthand. Async so stated for a function is asynchronous, and await an asynchronous method for waiting for completion of execution. In addition await only appear in the async function , async function returns a Promise object.

Problems encountered

In normal times, and if not the await the async, it is generally used than chaining, to receive parameters returned. When I did not use both, while the use of the structure of promise when not directly receive parameters returned because the object is a promise, not read, so this kind of asynchronous operation, we had better use the async and await, or chain than to deal with.

async role

Description is an asynchronous function, indicating the start of an asynchronous operation.

The role of await

async await for waiting for a return value of a function is asynchronous code, like with synchronized operation.

Examples

async function test(){
    try{
        let n = await getNum()
        console.log(n)
    }catch(error){
        console.log(error)
    }
}

thank

Universal Network companion

And a hardworking own

Published 161 original articles · won praise 331 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_45163122/article/details/104924063
Recommended