javascript 承诺异步或同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 承诺异步或同步相关的知识,希望对你有一定的参考价值。

//sync  sequential execution
Promise.resolve()
    .then(function(){
        return promotionDistributionModel.deleteStoreCampaignTargets(dbName, campaignId, data.organizationId)
    })
    .then(function(){
        return  promotionDistributionModel.addStoreCampaignTargets(dbName, campaignId, targets)
    })
    .then(function(){
        return promotionDistributionModel.deleteStoreCampaignStatus(dbName, campaignId, data.organizationId)
    })
    .then(function(){
        return promotionDistributionModel.addStoreCampaignStatus(dbName, campaignId, data.organizationId)
    })
    .then(trx.commit)
    .catch(trx.rollback);



//async  execute in random order
 Promise.all([
     promotionDistributionModel.deleteStoreCampaignTargets(dbName, campaignId, data.organizationId),
     promotionDistributionModel.addStoreCampaignTargets(dbName, campaignId, targets),
     promotionDistributionModel.deleteStoreCampaignStatus(dbName, campaignId, data.organizationId),
     promotionDistributionModel.addStoreCampaignStatus(dbName, campaignId, data.organizationId)
   ])
   .then(trx.commit)
   .catch(trx.rollback);

以上是关于javascript 承诺异步或同步的主要内容,如果未能解决你的问题,请参考以下文章

javascript中事件处理程序的异步或同步调用

异步 JavaScript - 回调与延迟/承诺 [重复]

Javascript - 异步等待和获取 - 返回值,而不是承诺?

如何调试 javascript 承诺?

javascript 承诺和异步等待

javascript 回调,承诺,异步/等待