本文介绍了我正在使用流星,我该怎么做才能等待从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世界而没有问候语.

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>

在客户端打印日志

Error: undefined Result: {}

在服务器端打印日志

[TypeError: Object [object Promise] has no method 'apply']

有什么主意我可以等待那个诺言而不返回空白的结果吗?

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

推荐答案

我假设balanced.marketplace.customers.create返回Promises/A + Promise.这是带有方法.then(fulfillmentCallback, rejectionCallback)的对象-操作成功时将调用fulfillmentCallback,如果操作出错则将调用rejectionCallback.这是您可以如何使用期货同步从承诺中获取价值的方法:

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)以获得一个承诺,然后对该承诺调用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.

这篇关于我正在使用流星,我该怎么做才能等待从API调用返回的诺言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:24