Reputation: 113
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
Upvotes: 5
Views: 950
Reputation: 4834
Nowadays the ES7 Async/Await is there to help the "callback trouble". See explained very well here: http://rossboucher.com/await/#/10
async function () {
let result1 = await Do.Something(1);
let result2 = await Do.Something(2);
}
Upvotes: 0
Reputation: 1498
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
Upvotes: 1
Reputation: 8072
Libraries like AsyncJS https://github.com/caolan/async are a general answer to the "callback pyramid of doom" problem.
Basically the replace calls like
Do.Something(1, function(result) {
Do.SomethingElse(2, function(result) {
Do.YetSomethingElse(3, function(result) {
....
})
})
})
With
asyncjs.series([
function F1(callback) {
Do.Something(1, callback);
},
function F1(callback) {
Do.SomethingElse(2, callback);
},
..... etc
])
Upvotes: 0