【问题标题】:Difference in behavior of 2 promises2 个承诺的行为差异
【发布时间】:2020-10-12 19:28:02
【问题描述】:

我想摆脱 q 库。 第一个 sn-p 有效,第二个无效。 这两个功能有区别吗?

this.removeAll = function (db) {
   var def = Q.defer();
   db.collection(collectionName).deleteMany({})
       .then(success => {
           def.resolve(success);
       }, error => {
           def.reject(error);
       })
   return def.promise
}
this.removeAll = function (db) {

    return db.collection(collectionName).deleteMany({})
        .then(success => {
            resolve(success);
        }, error => {
            reject(error);
        })
}

【问题讨论】:

    标签: promise q


    【解决方案1】:

    在您的第二个 sn-p 中没有可调用的 resolvereject 函数。

    为避免deferred antipattern,您应该放弃整个thencatch 调用:

    this.removeAll = function (db) {
        return db.collection(collectionName).deleteMany({});
    };
    

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 2017-12-18
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多