Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding callback hell. Meteor.call promise

I've been trying to avoid callback hell in Meteor but first, I'll explain my problem:

I have some Meteor methods declared in server, and I invoke them using Meteor.call in client, but the main problem is I have tons of callbacks making debugging a really difficult task to make (and manteinance too...). This wouldn't be an issue if I work with a "small" project, but I'm building a big one and I was planning on using promises but for my surprise... it's not working since Meteor.call only accepts callbacks.

I've read a lot of posts here and in Meteor forums and none of them can help me... is there any solution to this? (I tried deanius:promise package and it's still the same...)

Edit: using Meteor 1.4.2

like image 995
Droontar Avatar asked Nov 09 '22 06:11

Droontar


1 Answers

You can use the bluebird module for this. promisifyAll lets you convert all functions on an object to use promises instead of callbacks, so instead of using Meteor.call with a callback, you can use Meteor.callAsync as a promise.

With callbacks:

Meteor.call(..., function(...) {
    // ...
});

With promises:

Meteor.callAsync(...).then(function(...) {
    // ...
});

http://bluebirdjs.com/docs/api/promise.promisifyall.html

like image 176
afuous Avatar answered Nov 15 '22 06:11

afuous