Javascript 如何在js中创建钻石形状的承诺链
我被承诺束缚住了 我的程序结构是这样的Javascript 如何在js中创建钻石形状的承诺链,javascript,node.js,ecmascript-6,promise,es6-promise,Javascript,Node.js,Ecmascript 6,Promise,Es6 Promise,我被承诺束缚住了 我的程序结构是这样的 Func A //gets data then passes to I1 and J2 / \ Func I1 Func J1 //then route I & J run without interaction | | Func I2 Func J2
Func A //gets data then passes to I1 and J2
/ \
Func I1 Func J1 //then route I & J run without interaction
| |
Func I2 Func J2
\ /
Func B //Func B gets both the result of both
我有点麻烦让这个工作。
我到目前为止
getdata.then(data=>{
var methods = Promise.all([
funci1.x(data).then(output=>{
funci2.x(output)
}),
funcj1.x(data).then(output2=>{
funcj2.x(output2)
})
])
methods.then(([a,b])=>{
console.log(a,b);
})
})
但它似乎不起作用。有什么帮助吗?在您的两次
then()
回调中,您不会返回任何可以转换为承诺的内容,因此将其更改为:
getdata.then(data => {
var methods = Promise.all([
funci1.x(data).then(output => funci2.x(output)),
funcj1.x(data).then(output2 => funcj2.x(output2))
])
methods.then(([a, b]) => {
console.log(a, b);
})
})
在您的两个then()
回调中,您不会返回任何可以强制转换为Promise
的内容,因此将其更改为:
getdata.then(data => {
var methods = Promise.all([
funci1.x(data).then(output => funci2.x(output)),
funcj1.x(data).then(output2 => funcj2.x(output2))
])
methods.then(([a, b]) => {
console.log(a, b);
})
})
我个人觉得用这种方式写的承诺更容易确定发生了什么
const funcJ1 = (num) => {
num = num + 1;
return funcJ2(num);
}
const funcJ2 = (num) => (
new Promise((resolve, reject) => {
num = num + 1;
resolve(num);
})
);
const funcI1 = (num) => {
num = num + 1;
return funcI2(num);
}
const funcI2 = (num) => (
new Promise((resolve, reject) => {
num = num + 1;
resolve(num);
})
);
const funcB = (result) => {
let total = 0;
total = total + result[0]; // first promise result
total = total + result[1]; // second promise result
console.log(total);
};
const funcA = (x) => {
const promises = [];
promises.push(funcJ1(x));
promises.push(funcI2(x));
Promise.all(promises).then((res) => {
funcB(res); // done with I and J
}).catch((e) => {
throw e;
});
}
funcA(1);
funcJ1和funcI1将并行运行,funcB将在funcJ2和funcI2完成后运行
看到这个jsfiddle我个人觉得用这种方式写的承诺更容易识别发生了什么
const funcJ1 = (num) => {
num = num + 1;
return funcJ2(num);
}
const funcJ2 = (num) => (
new Promise((resolve, reject) => {
num = num + 1;
resolve(num);
})
);
const funcI1 = (num) => {
num = num + 1;
return funcI2(num);
}
const funcI2 = (num) => (
new Promise((resolve, reject) => {
num = num + 1;
resolve(num);
})
);
const funcB = (result) => {
let total = 0;
total = total + result[0]; // first promise result
total = total + result[1]; // second promise result
console.log(total);
};
const funcA = (x) => {
const promises = [];
promises.push(funcJ1(x));
promises.push(funcI2(x));
Promise.all(promises).then((res) => {
funcB(res); // done with I and J
}).catch((e) => {
throw e;
});
}
funcA(1);
funcJ1和funcI1将并行运行,funcB将在funcJ2和funcI2完成后运行
请参阅此JSFIDLE主要问题是使用箭头函数。为了澄清这一点,()=>{…}
执行大括号内的代码,并且仅当存在显式的return
语句时才返回值()=>语句
另一方面隐式返回语句的结果
看起来更像梯形(辛普森参考)。主要问题是使用箭头函数。为了澄清这一点,()=>{…}
执行大括号内的代码,并且仅当存在显式的return
语句时才返回值()=>语句
另一方面隐式返回语句的结果
看起来更像梯形(辛普森参考)如何从func的数据中运行j1和i1 aI已修改并向funcA添加了一个传递的参数。希望有帮助。如何从func的数据运行j1和i1 aI已修改并向func添加了一个参数,该参数已传递。希望有帮助。