简体   繁体   中英

Using Await While Looping a Map Object

您如何迭代Map中的每个值,以在该循环的主体中尊重await的方式?

I ultimately used map.values() within a for await loop.

Here's a contrived example that showcases await working within a loop:

 async function delay(ms) { console.log(`Waiting ${ms} milliseconds . . .`); return new Promise((resolve)=>{ setTimeout(resolve, ms); }); } async function main() { let map = new Map([[1,"A"],[2,"B"],[3,"C"]]); for await (const value of map.values()) { await delay(2000); console.log(value); } } main();

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