等待承诺在循环内完成

时间:2021-02-01 15:25:20

标签: for-loop async-await es6-promise

如何等待一个 promise 在循环内完成。

我在一个将执行大约 100 次的循环中有大约 10 个承诺。 对于每个承诺,我都想等到它被执行,因为它的结果将作为输入来执行下一行。

我怎样才能做到这一点?

for (const d of response.data.data)
                        {
                            console.log('BID ID - ' + d.id, '|| FPI ID - ' + d.attributes.fpi_id );
                            
                            let ifDealExistInDealList = await checkforBidID_fromDealList(d.id, context);
                            let ifDealExistInProgList = await checkforProgramIDforFPIID_fromProgList(d.attributes.fpi_id, context);
                            let ifDealExistInStrandList = await checkforStrandIDforFPIID_fromStrandList(d.attributes.fpi_id, context);
                                
                            if(ifDealExistInDealList === true)
                            {
                                let contractSignDate =  await get_data_CILS_ContractsDealAPI(d.attributes.fpi_id, context, token);
                                await updateExistingBid_inDealList(context, d, contractSignDate);
                            }
                            else if(ifDealExistInDealList === false && ifDealExistInProgList === true && ifDealExistInStrandList === false)
                            {
                                let contractSignDate =  await get_data_CILS_ContractsDealAPI(d.attributes.fpi_id, context, token);

                                let proGram = await getProgramIDforFPIID_fromProgList(d.attributes.fpi_id, context);   
                                if(proGram !== null)
                                {
                                    let proGramID = proGram[0].StrandID ;
                                    let title = proGram[0].Title ;

                                    let CustDealID = await createCustomID_Deal(context);
                                
                                    await addItemTO_DealList(proGramID, null, title, CustDealID, contractSignDate, d, context);
                                }
                            }
                            else if( ifDealExistInDealList === false && ifDealExistInProgList === false && ifDealExistInStrandList === true)
                            {
                                let contractSignDate =  await get_data_CILS_ContractsDealAPI(d.attributes.fpi_id, context, token);

                                let strand = await getStrandIDforFPIID_fromStrandList(d.attributes.fpi_id, context);  
                                if(strand !== null)
                                {
                                    let strandID = strand[0].StrandID ;
                                    let title = strand[0].Title ;
                                    
                                    let CustDealID = await createCustomID_Deal(context);
                                
                                    await addItemTO_DealList(null, strandID, title, CustDealID, contractSignDate, d, context);
                                }
                            }
                        }

0 个答案:

没有答案