如何使用流星允许3个电话承诺
问题描述:
我想避免回调地狱,所以我创立了承诺,但我有点卡住了。如何使用流星允许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
这里。我需要多一点帮助,请
答
所以尝试,并试图之后我做了这样的代码:
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个不同的因素
-
为
- 错误的做法
- 错误的参考,同时使用异步代码
Promise
完整修复:
首次使用需要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();
}
});
})处理您的异步代码;
你确定你使用的是** VAR无极=需要( '蓝鸟'); **? – ymz
@ymz确定这实际上解决了错误!但是代码是否正确?我的意思是每个电话中的'流星'是什么? – Jerome