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
  • 113
  • 2
  • 13

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
  • 630,263
  • 148
  • 957
  • 1,375