nodal icon indicating copy to clipboard operation
nodal copied to clipboard

Promises vs. callbacks

Open pptp opened this issue 8 years ago • 2 comments

Hi! You are using a passing of callback for supporting asynchrony

For example:

save(callback)

It means this callback will be called after saving.

I guess it will be more easier to use Promises for these purposes. For example. We should type the following code:

Model.find(this.params.route.id, (err, model) => {
    this.respond(err || model);
});

So, using Promises we will be able to code as follows:

Model.find(this.params.route.id)
  .then(
    this.respond.bind(this),
    this.someErrorLogFunc.bind(this)
  )

As far as I know, Node supports Promises since the version 0.12. For earlier versions there are shims.

Are you going to use Promises in the Nodal in near future?

Thanks, Mike

pptp avatar Jan 08 '17 11:01 pptp

Q, promises worked really well for the basic crud operations, except for query. The .end() function somehow gets in the way. And I had some issues when trying to nest promises, because the Model's need to be reconverted back to objets.

eperiou avatar Apr 10 '17 19:04 eperiou

+1 to @pptp. Async functions (aka async/await) is done and will come with the next ECMAScript release. So, I think we can benefit a lot if we use promises and async/await.

const obj = await Model.find({ ... });

gugadev avatar May 04 '17 13:05 gugadev