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

查看:146
本文介绍了如何在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?

谢谢。

推荐答案

一般解决方案:



客户端:



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

无法直接从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 NOT WOMOMENDED ,请参阅:

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

  • https://www.eventedmind.com/feed/meteor-what-is-meteor-bindenvironment
  • @imslavko comment from 18.07.2014
  • @Akshat answer : What's going on with Meteor and Fibers/bindEnvironment()?

有使用异步实用程序的第3种方法

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

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