簡體   English   中英

如何使用 Meteor 承諾撥打 3 個電話

[英]How to make 3 calls using Meteor promise

我想避免回調地獄,所以我建立了承諾,但我有點卡住了。

我需要讓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' }

[EDIT2] 按照@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 我需要更多幫助

所以在嘗試和嘗試之后我做了這個代碼:

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 個不同的因素而失敗

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

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM