async
async
The function returns a Promise object that can be used then
to add callback method. When the function is executed, the event await
will return to the first, until the asynchronous operation is complete, followed by execution statement after the function in vivo.
function setNumAdd(n) { return new Promise((resolve,reject) =>{ setTimeout( ()=>{ n +=1; resolve(n) },1000) }) } function setNumSub(n) { return new Promise((resolve,reject) =>{ setTimeout( ()=>{ n -=1; resolve(n) },1000) }) } async function d(n) { const w1 = await setNumAdd(n); const w2 = await setNumSub(n); return w1+w2 } d(10).then( v=>{ console.log(v) //10 })
async
The function returns a Promise object. async
Internal function return
value returned by the statement, will be the then
parameters of the method callback function.
async function f() { return 'hello world'; } f().then(v => console.log(v)) // "hello world"
async
Internal functions throw an error will result Promise returned object becomes the reject
state. Error object is thrown catch
method callback function received.
the async function F () { the throw new new Error ( 'wrong' ); } f().then( v => console.log(v), e => console.log(e) ) // equivalent to F () the then (V => the console.log (V)) the catch (E => the console.log (E)).. // Error: the error
await
Normally, await
a command is followed by a Promise object, and returns the result of the object. If the object is not Promise, directly returns the corresponding value.
the async function F () { // equivalent // return 123; return the await 123 ; } f().then(v => console.log(v)) // 123
Any await
statement following the Promise object becomes reject
a state, then the whole async
function will interrupt execution.
async function f() { Promise.reject the await ( 'wrong' ); Promise.resolve the await ( 'Hello World'); // not executed }
In the above code, the second await
statement is not executed, because the first await
statement into a state reject
.
Sometimes, we hope that even if an asynchronous operation failed before, and do not break the back of the asynchronous operation. Then you can be the first await
place try...catch
inside the structure, so that regardless of the asynchronous operation is successful, the second await
will be executed.
async function f() { try { Promise.reject the await ( 'wrong' ); } catch(e) { } return await Promise.resolve('hello world'); } f() .then(v => console.log(v)) // hello world
Another method is await
behind an object talk Promise a catch
method, error processing may occur in front of
async function f() { Promise.reject the await ( 'wrong' ) .catch(e => console.log(e)); return await Promise.resolve('hello world'); } f() .then (V => the console.log (V)) // wrong // Hello World Error Handling
If there are multiple await
commands can be placed in a unified try...catch
structure.
async function main() { try { const val1 = await firstStep(); const val2 = await secondStep(val1); const val3 = await thirdStep(val1, val2); console.log('Final: ', val3); } catch (err) { console.error(err); } }
let foo = await getFoo();
let bar = await getBar();
Multiple await
command asynchronous operation later, if there is no secondary relationship, it is best to let them triggered simultaneously.
The above code, getFoo
and getBar
two separate asynchronous operations (i.e. not mutually dependent), is written as secondary relationship. This is more time-consuming, because onlygetFoo
After completion, it will be implemented getBar
, so that they can simultaneously trigger.
// 写法一 let [foo, bar] = await Promise.all([getFoo(), getBar()]); // writing two the let fooPromise = getFoo (); let barPromise = getBar(); let foo = await fooPromise; let bar = await barPromise;