2017-02-09 46 views
2

我想避免回調地獄,所以我創立了承諾,但我有點卡住了。如何使用流星允許3個電話承諾

我需要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這裏。我需要多一點幫助,請

+1

你確定你使用的是** VAR無極=需要( '藍鳥'); **? – ymz

+0

@ymz確定這實際上解決了錯誤!但是代碼是否正確?我的意思是每個電話中的'流星'是什麼? – Jerome

回答

2

所以嘗試,並試圖之後我做了這樣的代碼:

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誰幫我

0

這個問題是非常棘手,因爲它失敗,原因是2個不同的因素

    Promise
  1. 錯誤的做法
  2. 錯誤的參考,同時使用異步代碼

完整修復:

首次使用需要Bluebird Promise lib:

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

})處理您的異步代碼;

+0

你忘了定義'call',我需要完成我的方法。在完成我的方法並測試之後,我會將這個標記爲答案。謝謝你的幫助:) – Jerome

+0

[編輯]我認爲你的答案有問題,請看看我的第二次編輯。最後一個問題,例如,我應該如何讓我的第二個方法'createDashboard'?我的意思是我應該調用像'whenDataArrive'?或者代碼如何知道'createDashboard'何時完成? – Jerome