Is it possible to resolve a promise in such a way that the next promise in the chain can be an executor that takes multiple parameters?
For example, say I have a function that takes three parameters:
function takesThreeParameters (a, b, c) {
console.log(`${a} ${b} ${c}`);
return 'something';
}
If I'm including it in a chain, e.g. one of these:
// case 1: in a new Promise
new Promise((resolve, reject) => resolve( /* ??? */ ))
.then(takesThreeParameters)
.then(console.log);
// case 2: in the middle of a chain somewhere
Promise.resolve()
.then(() => /* ??? */)
.then(takesThreeParameters)
.then(console.log);
// case 3: in the middle of a chain, but not as a function expression
function providesThreeValues () {
return /* ??? */;
}
Promise.resolve()
.then(providesThreeValues)
.then(takesThreeParameters)
.then(console.log);
Is there something I can return (in place of /* ??? */) in those cases (or at least in one of them) that will pass all three parameters in .then(takesThreeParameters)?
The important part of this question is if I can pass multiple parameters directly to an executor in a chain. So, strategies like these ones sort of bypass the question:
- Modifying
takesThreeParametersto take a single parameter - Resolving the previous promise with a dictionary then unpacking it (e.g.
abc => takesThreeParameters(abc.a, abc.b, abc.c)) - Same but with an array, etc. (
abc => takesThreeParameters(abc[0], abc[1], abc[2]))
I.e. I'm looking for a way to make things like .then((a, b, c) => /* code */) work in a chain.
I tried some hand-wavey things that, unsurprisingly, didn't work, e.g. with case 1:
resolve(2,4,6)results in: 2 undefined undefined, since resolve only takes one parameter.resolve((2,4,6))results in: 6 undefined undefined, as the value is a comma expression.resolve([2,4,6])results in: [2,4,6] undefined undefined, as expected.resolve({2,4,6})syntax error
I'm just using standard promises, whatever ships with Node.js v16.13.1 (which I think is ES6?).