1

I would like to get rid of q library. First snippet works, second does not. Is there a difference between these 2 functions?

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);
        })
}
Albert
  • 93
  • 1
  • 11

1 Answers1

1

There are no resolve and reject functions to call in your second snippet.

To avoid the deferred antipattern, you should drop the entire then and catch calls:

this.removeAll = function (db) {
    return db.collection(collectionName).deleteMany({});
};
Bergi
  • 572,313
  • 128
  • 898
  • 1,281