使用蓝鸟的一系列承诺在函数数组中传递参数
Passing parameters in an array of functions using a series of promises with bluebird
我有以下 Promise 功能:
const func1 = () => new Promise((resolve, reject) => {
console.log('func1 start');
setTimeout(() => {
console.log('func1 complete');
resolve('Hello');
}, 1000);
});
const func2 = (param_from_func1) => new Promise((resolve, reject) => {
console.log('func2 start ' + param_from_func1); // Want to get the value 'Hello' here
setTimeout(() => {
console.log('func2 complete');
resolve('World');
}, 2000);
});
为了连续执行这些函数,我使用:
const Promise = require('bluebird');
Promise.series = (promiseArr) => {
return Promise.reduce(promiseArr, (values, promise) => {
return promise().then((result) => {
values.push(result); // And here, I don't know how I can modify this to pass parameters between functions
return values;
});
}, []);
};
并致电:
Promise.series([func1, func2]).then(values => {
console.log("Promise Resolved. " + values); // Promise Resolved. Hello, World
}, function(reason) {
console.log("Promise Rejected. " + reason);
});
而且一切正常,因为我可以根据函数的顺序得到一系列值。
那么,我有两个问题:
如何将参数从 func1
传递到 func2
?
如何将参数初始传递给 func1
,然后再从它们传递给 func2
?
如果你想传递以前的结果,那么只需将 values
数组传播到 promise-returning 函数。替换:
promise().then
与:
promise(...values).then
const func1 = () => new Promise((resolve, reject) => {
console.log('func1 start');
setTimeout(() => {
console.log('func1 complete');
resolve('Hello');
}, 1000);
});
const func2 = (param_from_func1) => new Promise((resolve, reject) => {
console.log('func2 start ' + param_from_func1); // Want to get the value 'Hello' here
setTimeout(() => {
console.log('func2 complete');
resolve('World');
}, 2000);
});
Promise.series = (promiseArr) => {
return Promise.reduce(promiseArr, (values, promise) => {
return promise(...values).then((result) => {
values.push(result); // And here, I don't know how I can modify this to pass parameters between functions
return values;
});
}, []);
};
Promise.series([func1, func2]).then(values => {
console.log("Promise Resolved. " + values); // Promise Resolved. Hello, World
}, function(reason) {
console.log("Promise Rejected. " + reason);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.5.0/bluebird.min.js"></script>
[spread syntax
] 将数组替换为单独的参数,就像您单独列出它们一样。参见 spread syntax for function calls。
如果你还想传递一个值给第一个函数,那么你可以在传递给reduce
方法的初始值中指定它。所以然后替换:
, []);
与:
, [some_value]);
请注意,此值随后还将传递给第二个函数,该函数随后将接收两个参数而不是一个。如果您希望每个函数只接收一个值,即前一个承诺返回的值,而不是所有以前的承诺,则替换
promise(...values)
和
promise(values[values.length-1]).
这样一来他们都得到了一个值。
我有以下 Promise 功能:
const func1 = () => new Promise((resolve, reject) => {
console.log('func1 start');
setTimeout(() => {
console.log('func1 complete');
resolve('Hello');
}, 1000);
});
const func2 = (param_from_func1) => new Promise((resolve, reject) => {
console.log('func2 start ' + param_from_func1); // Want to get the value 'Hello' here
setTimeout(() => {
console.log('func2 complete');
resolve('World');
}, 2000);
});
为了连续执行这些函数,我使用:
const Promise = require('bluebird');
Promise.series = (promiseArr) => {
return Promise.reduce(promiseArr, (values, promise) => {
return promise().then((result) => {
values.push(result); // And here, I don't know how I can modify this to pass parameters between functions
return values;
});
}, []);
};
并致电:
Promise.series([func1, func2]).then(values => {
console.log("Promise Resolved. " + values); // Promise Resolved. Hello, World
}, function(reason) {
console.log("Promise Rejected. " + reason);
});
而且一切正常,因为我可以根据函数的顺序得到一系列值。
那么,我有两个问题:
如何将参数从
func1
传递到func2
?如何将参数初始传递给
func1
,然后再从它们传递给func2
?
如果你想传递以前的结果,那么只需将 values
数组传播到 promise-returning 函数。替换:
promise().then
与:
promise(...values).then
const func1 = () => new Promise((resolve, reject) => {
console.log('func1 start');
setTimeout(() => {
console.log('func1 complete');
resolve('Hello');
}, 1000);
});
const func2 = (param_from_func1) => new Promise((resolve, reject) => {
console.log('func2 start ' + param_from_func1); // Want to get the value 'Hello' here
setTimeout(() => {
console.log('func2 complete');
resolve('World');
}, 2000);
});
Promise.series = (promiseArr) => {
return Promise.reduce(promiseArr, (values, promise) => {
return promise(...values).then((result) => {
values.push(result); // And here, I don't know how I can modify this to pass parameters between functions
return values;
});
}, []);
};
Promise.series([func1, func2]).then(values => {
console.log("Promise Resolved. " + values); // Promise Resolved. Hello, World
}, function(reason) {
console.log("Promise Rejected. " + reason);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.5.0/bluebird.min.js"></script>
[spread syntax
] 将数组替换为单独的参数,就像您单独列出它们一样。参见 spread syntax for function calls。
如果你还想传递一个值给第一个函数,那么你可以在传递给reduce
方法的初始值中指定它。所以然后替换:
, []);
与:
, [some_value]);
请注意,此值随后还将传递给第二个函数,该函数随后将接收两个参数而不是一个。如果您希望每个函数只接收一个值,即前一个承诺返回的值,而不是所有以前的承诺,则替换
promise(...values)
和
promise(values[values.length-1]).
这样一来他们都得到了一个值。