The correct way to store permitted promises?

Currently, almost every node package has an api callback. What if I write a wrapper around, ORM, for example:

const Promise = require('bluebird');
const mongo = Promise.promisifyAll(require('mongodb'));

class ORM {
  constructor(mongoUrl) {
    this.db = mongo.connectAsync(mongoUrl);
  }

  collection(name) {
    return this.db.then((db) => {
      return db.collectionAsync(name);
    });
  }
}

Is it correct?

+4
source share
1 answer

Well, a promise is just a proxy for a value representing a value + time.

What you do is beautiful. In the order in which promises are stored, it is usually generally better to store promises rather than the values ​​they store, because this way you get much less race conditions.

, , - . , - , , . , .

, :

this.db = mongo.connectAsync(mongoUrl);
this.db.catch(() => {}); 

, , , , , , .

+1