I'm using Meteor, what do I need to do to wait for a promise to be returned from an API call?

前端 未结 3 1530
闹比i
闹比i 2020-12-21 23:37
if (Meteor.isClient) {

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

      //create a new customer
      Meteor.call(\'createCustomer\', function (e         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 00:01

    Another approach is to use Futures. I use this a lot on the server side to wait for results to return back to the client.

    Here's a small example of that I use for logins:

    Accounts.login(function (req, user) {
        var Future = Npm.require("fibers/future");
        var fut = new Future();
        HTTP.call("POST", globals.server + 'api/meteor-approvals/token',
            {
                timeout: 10000, followRedirects: true,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                params: {
                    username: userName,
                    password: req.password
                }},
            function (err, result) {
                if (err) {
                    logger.error("Login error: " + err);
                    fut.throw(err);
                }
                else {
                    fut.return("Success");
                }
            }
        );
        return fut.wait();
    }
    

提交回复
热议问题