【问题标题】:Promise with multiple .then when one then is counting on another承诺多个 .then 当一个 then 指望另一个
【发布时间】:2019-02-20 14:37:19
【问题描述】:

我试图在通过猫鼬访问mongodb 时隐式使用promise-then。 如果我有相同日期和用户的新数据,我应该删除旧数据并插入新数据。

A 部分工作正常,但我认为.then 的全部意义在于防止缩进,就像我们在回调方法中所做的那样。 另外,在这种情况下,我不知道是否需要为每个.then 提供.catch

但是 B 部分不起作用,因为在 .then(function(createdData){}) 中返回的 createdData 引用了 DailyData.remove() 方法,并返回一个完全不同的对象。

答:

DailyData.remove({date: getCurrentDate(), owner: currUser})
    .then(function(){
        DailyData.create({date: getCurrentDate(), owner: currUser})
        .then(function(createdData){
            createdDataGlobal = createdData;
                UpdatedInnerData.remove({date: getCurrentDate(), owner: currUser})
                .then(function(){
                    insertNewInnerData();
                })
                .catch(handleError);
        })
        .catch(handleError);
    })
    .catch(handleError);

乙:

DailyData.remove({date: getCurrentDate(), owner: currUser})
    .then(function(){
        DailyData.create({date: getCurrentDate(), owner: currUser});
    })
    .then(function(createdData){
        createdDataGlobal = createdData;
        UpdatedInnerData.remove({date: getCurrentDate(), owner: currUser});
    })
    .then(function(createdData){
        insertNewInnerData();
    })
    .catch(handleError);

【问题讨论】:

    标签: node.js mongodb mongoose promise


    【解决方案1】:

    前面的答案已经指出了问题所在,即您没有在.then(..) 中返回承诺 为了使代码更具可读性,您可以使用箭头函数。这样你就不需要显式返回:

    DailyData.remove({date: getCurrentDate(), owner: currUser})
    .then(() => DailyData.create({date: getCurrentDate(), owner: currUser})).then(() => ...)
    

    【讨论】:

      【解决方案2】:

      您没有在.then(..) 中返回承诺

      试试这个:

      DailyData.remove({date: getCurrentDate(), owner: currUser})
          .then(function(){
              return DailyData.create({date: getCurrentDate(), owner: currUser});
          })
          .then(function(createdData){
              createdDataGlobal = createdData;
              return UpdatedInnerData.remove({date: getCurrentDate(), owner: currUser});
          })
          .then(function(createdData){
              insertNewInnerData();
          })
          .catch(handleError);
      

      【讨论】:

        【解决方案3】:

        不合理的嵌套thens 是一种反模式。这称为回调地狱;承诺应该帮助但不是原因的事情。

        另一个问题是承诺应该总是被链接起来。 DailyData.create(...) 不会从then 回调中返回,这会导致控制流不正确。

        应该是:

        DailyData.remove({date: getCurrentDate(), owner: currUser})
        .then(function(){
            return DailyData.create({date: getCurrentDate(), owner: currUser});
        })
        .then(function(createdData){
            createdDataGlobal = createdData;
            return UpdatedInnerData.remove({date: getCurrentDate(), owner: currUser});
        })
        .then(function(createdData){
            return insertNewInnerData();
        })
        .catch(handleError);
        

        如果insertNewInnerData() 是异步的但不返回一个promise,则应该修改它以返回它。

        不需要多个catch,除非必须以不同方式处理错误。

        【讨论】:

          猜你喜欢
          • 2015-01-14
          • 1970-01-01
          • 2021-03-05
          • 2019-09-08
          • 2018-03-10
          • 1970-01-01
          • 2015-10-16
          • 2018-03-18
          • 1970-01-01
          相关资源
          最近更新 更多