我想将一系列的诺言链接起来,以便依次执行三个诺言。我不想嵌套功能。我尝试向其中添加一个解析,但似乎在功能内部的所有操作完成之前它都已解析。
function first(){
var promise = new Promise(function(resolve, reject){
console.log('fetchToken 1');
});
return promise;
}
function second(item){
var promise = new Promise(function(resolve, reject){
console.log('fetchToken 2');
});
return promise;
}
function third(item){
var promise = new Promise(function(resolve, reject){
console.log('fetchToken 3');
});
return promise;
}
function getTokenGroup() {
function logTaskError(e) {
console.error(e);
throw e; // reject the Promise returned by then
}
var task1 = first();
var task2 = task1.then(second);
var task3 = task2.then(third);
var alltasks = task3.then(null, logTaskError);
return alltasks;
}
getTokenGroup();
最佳答案
您需要致电“解决”:
function first(){
var promise = new Promise(function(resolve, reject){
console.log('fetchToken 1');
resolve(1); //==> call this one, parameter in here is just a sample
});
return promise;
}
function second(item){
var promise = new Promise(function(resolve, reject){
console.log('fetchToken 2');
});
return promise;
}
function third(item){
var promise = new Promise(function(resolve, reject){
console.log('fetchToken 3');
});
return promise;
}
function getTokenGroup() {
function logTaskError(e) {
console.error(e);
throw e; // reject the Promise returned by then
}
var task1 = first();
var task2 = task1.then(second);
var task3 = task2.then(third);
var alltasks = task3.then(null, logTaskError);
return alltasks;
}
getTokenGroup();
关于javascript - 如何串联 promise ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43578221/