- Objective: - Breadcrumb: # 概念阐释 ## 语义 [[js 异步 Promise.all()并发处理]]的语法糖格式。 ## 语法 ### print时写`await` ```js async function concurrent() { const firstPromise = firstAsyncThing(); const secondPromise = secondAsyncThing(); console.log(await firstPromise, await secondPromise); } ``` ### `await Promise.all()` ```js async function asyncPromAll() {   const resultArray = await Promise.all([asyncTask1(), asyncTask2(), asyncTask3(), asyncTask4()]);   for (let i = 0; i<resultArray.length; i++){     console.log(resultArray[i]);   } }   asyncPromAll(); ``` # 实例 ```js const serveDinner = async () => { const vegetablePromise = steamBroccoli(); const starchPromise = cookRice(); const proteinPromise = bakeChicken(); const sidePromise = cookBeans(); console.log(`Dinner is served. We're having ${ await vegetablePromise}, ${ await starchPromise}, ${ await proteinPromise}, and ${ await sidePromise}.`) }; serveDinner(); ``` ```js async function serveDinnerAgain(){ // Promise.all的值为数组 let foodArray = await Promise.all([steamBroccoli(), cookRice(), bakeChicken(), cookBeans()]); console.log(`We're having ${foodArray[0]},${foodArray[1]},${foodArray[2]},${foodArray[3]}.`); }; serveDinnerAgain(); ``` # 相关内容 注意:如果我们有多个真正独立的承诺,我们想要完全并行执行,我们必须使用单独的.then()函数,并避免使用await停止我们的执行。 # 问题 # 问题答案 # 参考资料 - [handling independent promiises](https://www.codecademy.com/journeys/full-stack-engineer/paths/fscj-22-front-end-development/tracks/fscj-22-async-javascript-and-http-requests/modules/wdcp-22-learn-javascript-syntax-async-await-40c16baa-3867-4d62-917d-5b66da1a85f5/lessons/async-await/exercises/concurrency) - [await promise.all](https://www.codecademy.com/journeys/full-stack-engineer/paths/fscj-22-front-end-development/tracks/fscj-22-async-javascript-and-http-requests/modules/wdcp-22-learn-javascript-syntax-async-await-40c16baa-3867-4d62-917d-5b66da1a85f5/lessons/async-await/exercises/await-promise-all)