【问题标题】:How to make 3 calls using Meteor promise如何使用 Meteor Promise 拨打 3 个电话
【发布时间】:2017-02-09 20:14:23
【问题描述】:

我想避免回调地狱,所以我创建了承诺,但我有点卡住了。

我需要制作getAllDataSource -> createDashboard-> `sendDashboard``

所以代码是:

var call = Promise.promisify(Meteor.call, Meteor);

var calls = call(getAllDataSource()).
            then(call.bind(Meteor, createDashboard())).
            then(call.bind(Meteor, sendDashboard()));

calls.then(function(resThree){
    console.log("Got Response!", resThree);
}).catch(function(err){
    console.log("Got Error", err); 
});

但我对第一个 var call有点迷茫,我想我需要改变它,但是用什么?然后这将如何知道getAllDataSource何时完成?

var allDataSources;
getAllDataSources = Meteor.bindEnvironment(function(){
    HTTP.call("GET", 'http://localhost:3000/api/datasources', {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'Bearer eyJrIjoic2RRbU9oM2Rkbmc0bHZUSjVlTjBPckRVNlplSW1DYzEiLCJuIjoibG9jYWxob3N0X2FkbWluX2tleSIsImlkIjoxfQ==',
            },
        },
        function(error, result) {
            if (!error) {
              allDataSources = result.data;
            } else {
                console.error(error);
            }
        });
});

var sendme;
createDashboard = Meteor.bindEnvironment(function(){
  for (var i = 0; i < 5; i++) {
    console.log("I have " + i + " apples in " + allDataSources);
    sendme = "hihihih";
  }
});

sendDashboard = Meteor.bindEnvironment(function(){
  for (var i = 0; i < 7; i++) {
    console.log("I have " + i + " cats with " + sendme);
  }
});

创建结果时会自动转到方法 2 吗?

感谢您的帮助

[编辑] 这实际上是在控制台上给我的:

Got Error { [Error: Method 'undefined' not found [404]]
I20170209-10:39:30.990(1)?   error: 404,
I20170209-10:39:30.991(1)?   reason: 'Method \'undefined\' not found',
I20170209-10:39:30.991(1)?   details: undefined,
I20170209-10:39:30.991(1)?   message: 'Method \'undefined\' not found [404]',
I20170209-10:39:30.991(1)?   errorType: 'Meteor.Error' }

[编辑2] 在遵循@ymz 的答案后,我得到了这个错误:

Got Error { [Error: Method '[object Object],[object Object],[object Object],[object Object]' not found [404]]
I20170209-11:23:48.154(1)?   error: 404,
I20170209-11:23:48.154(1)?   reason: 'Method \'[object Object],[object Object],[object Object],[object Object]\' not found',
I20170209-11:23:48.154(1)?   details: undefined,
I20170209-11:23:48.154(1)?   message: 'Method \'[object Object],[object Object],[object Object],[object Object]\' not found [404]',
I20170209-11:23:48.154(1)?   errorType: 'Meteor.Error' }

我认为它来自var calls = call(data).then .... // proceed from here,因为getAllDataSource()data 中放置了一个数组。我需要更多帮助

【问题讨论】:

  • 你确定你在使用 var Promise = require('bluebird');
  • @ymz 好的,这实际上解决了错误!但是代码对吗?我的意思是每次通话中的Meteor 是什么?

标签: javascript meteor promise


【解决方案1】:

所以在尝试和尝试之后,我制作了这个代码:

new Promise(function(resolve) {
  console.log("step1")
    // 1. first async task
    HTTP.call("GET", 'http://localhost:3000/api/datasources', {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'Bearer 123',
            },
        },
        function(error, result) {
            if (!error) {
              allDataSources = result.data;
              console.log("step1.5" + allDataSources)
              resolve(allDataSources);
            } else {
                console.error(error);
            }
        });

}).then(function(allDataSources) {
  console.log("step2")
    // 2. second async task
    return new Promise(function(resolve) {
      console.log("step 2.5" + resolve + allDataSources)
      for (var dataSource = 0; dataSource < allDataSources.length; dataSource++) {
         sendme = "sendme";
      }
      resolve(sendme);
    });

}).then(function(sendme) {
    // 3. now we can render the products after two async tasks are done
    console.log('Rending product ' + sendme);
});

非常感谢帮助我的@ymz

【讨论】:

    【解决方案2】:

    这个问题很棘手,因为它因 2 个不同的因素而失败

    1. Promise 的引用错误
    2. 使用异步代码时方法错误

    完整的修复:

    首先 - 对 Bluebird Promise 库使用 require:

    var Promise = require('bluebird');
    

    第二 - 用回调处理你的异步代码

    function whenDataArrive(data)
    {
        if (!data) return;
    
        var calls = call(data).then .... // proceed from here
    }
    
    getAllDataSources = Meteor.bindEnvironment(function(id){
    HTTP.call("GET", 'http://localhost:3000/api/datasources', {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'Bearer 123',
            },
        },
        function(error, result) {
            if (!error) {
              whenDataArrive(result.data);
            } else {
                console.error(error);
                whenDataArrive();
            }
        });
    

    });

    【讨论】:

    • 你忘了定义call,我需要完成我的方法。完成我的方法并测试它后,我会将其标记为答案。谢谢你的帮助:)
    • Edit 我认为您的回答有问题,请查看我的第二次编辑。最后一个问题,例如我应该如何制作我的第二种方法 createDashboard ?我的意思是我应该打电话给whenDataArrive 之类的东西?或者代码如何知道createDashboard 何时完成?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多