我想避免回调地狱,所以我创立了承诺,但我有点卡住了。如何使用流星允许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
这里。我需要多一点帮助,请
你确定你使用的是** VAR无极=需要( '蓝鸟'); **? – ymz
@ymz确定这实际上解决了错误!但是代码是否正确?我的意思是每个电话中的'流星'是什么? – Jerome