lazy icon indicating copy to clipboard operation
lazy copied to clipboard

Promisify nedb & request

Open anonrig opened this issue 9 years ago • 3 comments

Because of the lack of bluebird in this library, you had to promisify nedb from scratch. But basically, you could have used Promise.promisifyAll(Datastore.prototype);.

On the other hand, request-promise library already promisifies request library.

anonrig avatar Apr 26 '17 19:04 anonrig

First of all thank you very much for your contribution, It made me happy to share your information with me.

  • Promise.promisifyAll(Datastore.prototype); I'm asking because I do not really know. Does this solution solve it literally? This method will provide a more legible spelling/understanding if it really works completely. I did not examine this database in detail when I wrote the project.

On the other hand, I did not do that because the second solution will add extra dependency. request-promise

If you have any other ideas, I would like to talk and discuss fondly.

Thank you again for your contribution. Çağatay

cagataycali avatar Apr 26 '17 22:04 cagataycali

It's proven in the past that Bluebird's (https://github.com/petkaantonov/bluebird) Promise implementation is faster than the actual Promise in NodeJs.

Following code will promisify all functions that include callbacks. Therefore you don't need to do return new Promise((res, rej) every time.

const Promise = require('bluebird');
Promise.promisifyAll(Datastore.prototype);

For example: you can just use the following code:

return this.categories.findOne({name: obj.category});

On the other hand using Bluebird will enable this function:

quiet() {
    return new Promise(resolve => {
      this.slient = !this.slient;
      resolve(slient);
    });
  }

to be replaced by this:

quiet() {
  this.slient = !this.slient;
  return Promise.resolve(this.slient);
}

On the other hand, when promisifying an actual class/function that you didn't write from scratch, developers tend to make mistakes. Therefore, adding request-promise will have more benefits for the library in the long term.

But again, it's your choice. Best.

anonrig avatar Apr 26 '17 23:04 anonrig

I'll rewrite lazy with your instructions in few days Thanks your advices 👍

cagataycali avatar Apr 27 '17 16:04 cagataycali