如何使用 Meteor 在函数中获取异步数据 [英] How to get an async data in a function with Meteor

查看:21
本文介绍了如何使用 Meteor 在函数中获取异步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Meteor 的新手,我正在尝试从 Heroku API 获取异步数据.

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

服务器端代码:

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;
        });
    }
});

客户端代码:

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

Heroku 需要一段时间来回答,所以答案是 undefined.

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

那么捕获异步结果的最佳方法是什么?

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

谢谢.

推荐答案

一般解决方案:

客户端:

    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);
            } );
        }
    }

无法直接从 Meteor.call 返回结果.但是,至少有 2 个解决方案(@akshat 和 @Hubert OG):如何在模板助手中使用 Meteor 方法

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

使用 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;
          }

      }
  });
}

正确使用 Future 库:

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();
        }
    });
}

使用 Future 库 WITHOUT Meteor.bindEnvironment 不推荐,请参阅:

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

还有使用 异步实用程序

这篇关于如何使用 Meteor 在函数中获取异步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆