Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Meteor promise拨打3个电话_Javascript_Meteor_Promise - Fatal编程技术网

Javascript 如何使用Meteor promise拨打3个电话

Javascript 如何使用Meteor promise拨打3个电话,javascript,meteor,promise,Javascript,Meteor,Promise,我想避免回调,所以我建立了承诺,但我有点被卡住了 我需要使getAllDataSource->createDashboard->`sendDashboard`` 因此,代码是: var call = Promise.promisify(Meteor.call, Meteor); var calls = call(getAllDataSource()). then(call.bind(Meteor, createDashboard())). the

我想避免回调,所以我建立了承诺,但我有点被卡住了

我需要使
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
调用有点迷茫
我想我需要改变它,但是用什么?然后如何知道
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);
  }
});
[编辑二] 在遵循@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)从这里开始
,因为
getAllDataSource()
在这里的
数据中放置了一个数组。我需要更多的帮助,请

这个问题很棘手,因为它由于两个不同的因素失败了

  • 承诺的错误引用
  • 使用异步代码时的错误方法
  • 完整修复:

    首先-使用蓝鸟承诺库:

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

    }))

    因此,在不断尝试之后,我编写了以下代码:

    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);
    });
    
    新承诺(函数(解析){
    控制台日志(“步骤1”)
    //1.第一个异步任务
    HTTP.call(“GET”http://localhost:3000/api/datasources', {
    标题:{
    “接受”:“应用程序/json”,
    “内容类型”:“应用程序/json”,
    “授权”:“持票人123”,
    },
    },
    函数(错误、结果){
    如果(!错误){
    allDataSources=result.data;
    console.log(“step1.5”+所有数据源)
    解析(所有数据源);
    }否则{
    控制台错误(error);
    }
    });
    }).then(函数(所有数据源){
    控制台日志(“步骤2”)
    //2.第二个异步任务
    返回新承诺(函数(解析){
    日志(“步骤2.5”+解析+所有数据源)
    对于(var-dataSource=0;dataSource

    我想向帮助我的@ymz表示衷心的感谢

    你确定你在使用var Promise=require('bluebird')@这实际上解决了这个错误!但是代码正确吗?我的意思是每次调用的
    Meteor
    是什么?你忘了定义
    call
    ,我需要完成我的方法。完成我的方法后,我会把这一个标记为答案并测试它。谢谢你的帮助:)我想你的答案有问题,请看我的第二次编辑。最后一个问题,例如,我应该如何创建第二个方法
    createDashboard
    ?我的意思是我应该调用类似于
    WhenDaarDrive
    ?或者代码如何知道何时将完成
    createDashboard
    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);
    });