我正在使用 Meteor,我需要做什么才能等待 API 调用返回承诺? [英] I'm using Meteor, what do I need to do to wait for a promise to be returned from an API call?

查看:18
本文介绍了我正在使用 Meteor,我需要做什么才能等待 API 调用返回承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (Meteor.isClient) {

  Template.hello.events({
    'click input': function () {

      //create a new customer
      Meteor.call('createCustomer', function (error, result) { 
        console.log("Error: " + error + "  Result: " + result); } );
    }
  });
}

if (Meteor.isServer) {
  Meteor.methods({
    createCustomer: function () {
      try {
      balanced.configure('MyBalancedPaymentsTestKey');
      var customer = Meteor._wrapAsync(balanced.marketplace.customers.create());
      var callCustomer = customer();
      var returnThis = console.log(JSON.stringify(callCustomer, false, 4));
      return returnThis;
    } catch (e) {
      console.log(e);
      var caughtFault = JSON.stringify(e, false, 4);
    }
    return caughtFault;
    }
  });
}

我只使用了默认的 hello world,没有问候行.

And I just used the default hello world without the greetings line.

<head>
  <title>testCase</title>
</head>

<body>
  {{> hello}}
</body>

<template name="hello">
  <h1>Hello World!</h1>
  <input type="button" value="Click" />
</template>

在客户端打印日志

错误:未定义结果:{}

在服务器端打印日志

[TypeError: Object [object Promise] 没有方法 'apply']

知道如何等待那个承诺而不是返回空白结果吗?

Any idea how I can wait for that promise instead of returning the blank result?

推荐答案

我假设 balanced.marketplace.customers.create 返回 Promises/A+ 承诺.这是一个带有方法.then(fulfillmentCallback,rejectionCallback)的对象——操作成功时调用fulfillmentCallback,调用rejectionCallback如果操作有错误.以下是如何使用 Futures 同步从 promise 中获取值的方法:

I'm assuming balanced.marketplace.customers.create returns a Promises/A+ promise. This is an object with a method .then(fulfillmentCallback, rejectionCallback) - the fulfillmentCallback is called when the operation succeeds, and the rejectionCallback is called if the operation had an error. Here's how you could use Futures to synchronously get the value out of a promise:

var Future = Npm.require("fibers/future");

function extractFromPromise(promise) {
  var fut = new Future();
  promise.then(function (result) {
    fut["return"](result);
  }, function (error) {
    fut["throw"](error);
  });
  return fut.wait();
}

然后你可以正常调用balanced.marketplace.customers.create(没有_wrapAsync)来得到一个promise,然后调用extractFromPromise承诺获得实际结果值.如果出现错误,则 extractFromPromise 将抛出异常.

Then you can just call balanced.marketplace.customers.create normally (no _wrapAsync) to get a promise, then call extractFromPromise on that promise to get the actual result value. If there's an error, then extractFromPromise will throw an exception.

顺便说一下,if (Meteor.isServer) 块中的代码仍然会发送到客户端(即使客户端没有运行它),所以你不想把你的API 密钥在那里.你可以把代码放到server目录下,这样Meteor就完全不会发送给客户端了.

By the way, code in if (Meteor.isServer) blocks is still sent to the client (even if the client doesn't run it), so you don't want to put your API key in there. You can put code in the server directory, and then Meteor won't send it to the client at all.

这篇关于我正在使用 Meteor,我需要做什么才能等待 API 调用返回承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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