Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

javascript - How to get an async data in a function with Meteor

I'm a newbie with Meteor and I'm trying to get an async data from the Heroku API.

Server side code:

heroku = Meteor.require("heroku");

Meteor.methods({
    'getHeroku': function getHeroku(app){
        client = new heroku.Heroku({key: "xxxxxx"});
        client.get_app(app, function (error, result) {
            return result;
        });
    }
});

Client side code:

Template.herokuDashboard.helpers({
    appInfo: function() {
        Meteor.call('getHeroku', "meathook-api", function (error, result) {
            console.warn(result);
        } );
    }
});

Heroku takes a while to answer so the answer is undefined.

So what is the best way to catch the async result?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

General solution :

Client Side:

    if (Meteor.isClient) {
        Template.herokuDashboard.helpers({
            appInfo: function() {
                return Session.get("herokuDashboard_appInfo");
            }
        });
        Template.herokuDashboard.created = function(){
            Meteor.call('getData', function (error, result) {
                Session.set("herokuDashboard_appInfo",result);
            } );
        }
    }

There is no way to directly return results from Meteor.call. However there are at least 2 solutions (@akshat and @Hubert OG): How to use Meteor methods inside of a template helper

Server Side (Meteor._wrapAsync):

Using Meteor._wrapAsync :

if (Meteor.isServer) {
  var asyncFunc = function(callback){
      setTimeout(function(){
          // callback(error, result);
          // success :
          callback(null,"result");
          // failure:
          // callback(new Error("error"));
      },2000)
  }
  var syncFunc = Meteor._wrapAsync(asyncFunc);
  Meteor.methods({
      'getData': function(){
          var result;
          try{
               result = syncFunc();
          }catch(e){
              console.log("getData method returned error : " + e);
          }finally{
              return result;
          }

      }
  });
}

Proper usage of Future library:

if (Meteor.isServer) {
    Future = Npm.require('fibers/future');

    Meteor.methods({
        'getData': function() {
            var fut = new Future();
            setTimeout(
                Meteor.bindEnvironment(
                    function() {
                        fut.return("test");
                    },
                    function(exception) {
                        console.log("Exception : ", exception);
                        fut.throw(new Error("Async function throw exception"));
                    }
                ),
                1000
            )
            return fut.wait();
        }
    });
}

Using Future library WITHOUT Meteor.bindEnvironment is NOT RECOMMENDED, see:

There is also 3rd approach using Async utilities


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...