How to call async method from Meteor own callbacks?

左心房为你撑大大i 提交于 2019-12-21 05:18:14

问题


I've just spent a few hours reading SO with answers such as Meteor: Calling an asynchronous function inside a Meteor.method and returning the result

Unfortunately, I still didn't manage to user fibers, or futures for that matter.

I'm trying to do something fairly simple (I think!).

When creating a user, add a variable to the user object, based on the result of an asynchronous method. So imagine if you will my async method is called on a 3rd party db server called BANK, which could take several seconds to return.

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 moved on and _.extend is never invoked.

I tried placing the return call inside the Fiber, that only made things worse: it never return user. Well it did, but 3 seconds too late so by then everything downstream was bailing out.

Thank you for any help!


回答1:


Answering my own question which hopefully will help some people in the future. This is based on the excellent advice of Avital Oliver and David Glasser to have a look at Mike Bannister's meteor-async.md. You can read it here: https://gist.github.com/possibilities/3443021

Accounts.onCreateUser(function(options,user){
    _.extend(user,{
        balance: getBalance(),
    });
  return user;
});


function getBalance() {
  var Future = Npm.require("fibers/future");
  var fut = new Future();
  BANK.getBalance(function(err, bal) {
    if (err) return console.log(err);
    fut.return(bal);
  });
  return fut.wait();
}

I believe there's an even better way to handle this, which is directly by wrapping the BANK API in Futures within the npm package itself, as per this example (from Avital Oliver): https://github.com/avital/meteor-xml2js-npm-demo/blob/master/xml2js-demo.js

I hope it helps!




回答2:


Use this.unblock() on server side code.

From Meteor 1.0 documentation: "Allow subsequent method from this client to begin running in a new fiber.On the server, methods from a given client run one at a time. The N+1th invocation from a client won't start until the Nth invocation returns. However, you can change this by calling this.unblock. This will allow the N+1th invocation to start running in a new fiber."

Meteor.methods({checkTwitter: function (userId) {
 check(userId, String);
 this.unblock();
 try {
   var result = HTTP.call("GET", "http://api.twitter.com/xyz",
                       {params: {user: userId}});
  return true;
 } catch (e) {
  // Got a network error, time-out or HTTP error in the 400 or 500 range.
 return false;
 }

}});




回答3:


method calls use the sync style (see 'sync call' here http://docs.meteor.com/#meteor_call) on the server side, which is where this create user method runs - you should be able to do something like

Accounts.onCreateUser(function(options, user) {
  user.balance = Meteor.call('getBankBalance', params);
  return user;
});



回答4:


Thanks yo so much that's work, This solution its better for Meteor projects, because Fibers module installed by default. mrt add npm has a method for this too -> Meteor.sync . For any nodeJS projects there is a other module based on Fibers, its name is Fibrous

Reference:https://github.com/goodeggs/fibrous



来源:https://stackoverflow.com/questions/16269507/how-to-call-async-method-from-meteor-own-callbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!