我想避免回调地狱,所以我建立了承诺,但我有点卡住了。
我需要制作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);
});
但是我对第一个变量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
放置了一个数组。我需要更多的帮助
答案 0 :(得分: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
答案 1 :(得分:0)
这个问题很棘手,因为它因两个不同因素而失败
Promise
完整修复:
首先 - 对Bluebird Promise lib使用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();
}
});
});