I just spent several hours reading SO with answers like Meteor: calling an asynchronous function inside Meteor.method and returning the result
Unfortunately, I still have not been able to use user threads or futures.
I'm trying to make something pretty simple (I think!).
When creating a user, add a variable to the user object based on the result of the asynchronous method. So imagine if you call my asynchronous method on a third-party db server called BANK, which can take a few seconds.
Accounts.onCreateUser(function(options,user){ var Fiber = Npm.require("fibers"); Fiber(function() { BANK.getBalance(function(err, theBalance) { if (err) return console.log(err); _.extend(user,{ balance: theBalance; }); }); }).run(); return user;
});
So what happens in the above is that the BANK method is called, but by the time it returns, the code has already been moved, and _.extend is never called.
I tried to place a callback inside Fiber, which only exacerbated the situation: it never returns the user. Well, yes, but 3 seconds is too late, so by then everyone downstream had been saved.
Thanks for the help!
source share