【发布时间】:2019-09-18 15:36:18
【问题描述】:
对于纯 Promise 函数,我有一个方便的解决方案。但我需要一种方法来链接一组承诺函数,最后一个“then”将调用其他三个非承诺函数。
以下是我尝试过的
function chainMyPromises(promiseFunction, promiseArray){
if(!promiseArray.length){
return;
}
promiseFunction.then(()=>{
const currentFunc = promiseArray.shift()
return chainMyPromises(currentFunc(), promiseArray);
});
}
const promiseArray = [functionOne, functionTwo, functionThree, functionFour];
function firstPromise(){
return Promise.resolve(true);
}
chainMypromises(firstpromise, promiseArray);
下面是我的函数,它有一些基于 promise 的函数,最后一个“then”检查响应并调用三个非基于 promise 的函数。
function consolidatedReport(param1, param2){
const somedata = param1.data;
const someOtherData = param2.data;
if(true){
doThisthing();
}
return promiseChainBegin(somedata, someOtherData)
.then(response => response && functionOne(somedata, someOtherData)
.then(response => response && functionTwo(somedata, someOtherData)
.then(response => response && functionThree(somedata, someOtherData)
.then(response => response && functionFour(somedata, someOtherData)
.then(response => {
if(response) {
notApromiseFuncOne(somedata)(someOtherData);
notApromiseFuncTwo(somedata)(someOtherData);
notApromiseFuncThree(somedata)(someOtherData);
} else{
notApromiseFailCase(someOtherData);
}
});
}
最后一个'then'调用三个不基于promise的函数。我需要一种方法来链接这些并整理它们。请提出重构它们的最佳方法。
【问题讨论】:
-
所以
notApromiseFuncOne等不是异步的吗?那么就没有问题了 -
@JaromandaX 怎么样?你能给我一个例子吗?如果我的 Util 函数都是异步函数,那么它可以正常工作,但因为最后我有非异步函数。在那里面对一个无赖。
标签: javascript reactjs ecmascript-6 promise