简体   繁体   中英

Could someone explain the behaviour of async-await here?

Using the code below I get a return value of result [object Promise]3. Could you explain me and put some light on what is this [object promise]3 is it an array of 3 promise objects? or something else?

 const add = (a,b) => { return new Promise((resolve,request) => { setTimeout(()=>{ resolve(a+b) },2000) }) } const doWork = async ()=> { const sum = await add(1, 99) const sum2 = await add(sum, 50) const sum3 = await add(sum2, 3) return sum3 } doWork().then((result)=>{ console.log('result', result) }).catch((e)=>{ console.log('error', e) })

outputs

sum2 is a promise. When you use + operator to add 3 to it, it calls the toString() on the promise and it returns [object Promise]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM