【问题标题】:Handle both resolved promises处理两个已解决的承诺
【发布时间】:2023-03-05 10:41:01
【问题描述】:

假设我有一个承诺,我使用 .then 等待它解决,

var suc = function(){ console.log('promise ended') } 
var err = function(e){ console.log('promise ended') }

promise.then( suc, err );
>> promise ended
>> promise ended

如果我不在乎它是成功还是失败怎么办?我只想知道它已经完成了。

promise.both(function(){console.log('next')}

有这样的功能吗?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    可以在调用sucerr 之前使用.then()

    var completed = function() { console.log('next') };
    
    promise.then(completed, completed).then(suc, err)
    
    【解决方案2】:

    你可以这样做:

    promise.catch(function() {}).then(function() { console.log('DONE'); });
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 2022-01-22
      • 2018-04-02
      • 2016-12-13
      • 2022-01-20
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      相关资源
      最近更新 更多