Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

node.js - How to ensure all async methods successfully executed

I am new in NODE-JS, stuck on basic functionality, I have four sequentially executed async methods, they must be executed sequentially. you may check POST event==>progression method.

I have 2 issues

1)How to set promise.all when there is any error raise want to rollback, is there any better approach please share with me.

2)It's a concurrently requested POST method, need to execute parallelly, or any best approach to handle concurrent method.

  Task-1 getCompanyAssociatedDeals
  Task-2 getDealInformation
  Task-3 createDeal
  Task-4 updateCompanyDeal


async function getCompanyAssociatedDeals(companyId) {
  try {    
    return result;
  } catch (error) {}
}

async function getDealInformation(dealId) {
  try {    
    return result;
  } catch (error) {}
}

async function createDeal(totalDealAmount, companyId) {
  try 
  {   
    return newMergeDeal;
  } catch (error) {
    console.log("Error on creation");
  }
}

async function updateCompanyDeal(totalDealAmount, companyId) {
  try 
  {   
    return updatedDeal;
  } catch (error) {
    console.log("Error on creation");
  }
}

app.post("/progression", async (req, res) => {
  
  const associatedDeals = await getCompanyAssociatedDeals(req.body.objectId);  

  let totalDealAmount = 0;
  for (let deal of associatedDeals.results) {
    let currentDeal = await getDealInformation(deal);
    totalDealAmount += Number(currentDeal.properties.amount.value);
  }
  
  if (totalDealAmount) {
    
    let newMergeDeal = await createDeal(totalDealAmount, req.body.objectId);    
    await updateCompanyDeal(totalDealAmount, req.body.objectId);
    
    return res.status(200).send("new Merge Deal " + newMergeDeal.dealId);
  }
  return res.status(200).send("success" + req.body.objectId);
});

Note: Task-1 output result needs to use in Task-2, Task-3 output result needs to used in Task-3 and Task-4.

Following Promise syntax may not help me to used one task output result to another one

await Promise.all([Task-1(),Task-2(),Task-3(),Task-4()]);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...