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
takesThreeParameters
to 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?).
CodePudding user response:
The resolve
callback of Promise
will just take the first parameter, so if you want to pass multiple values you need to pass an array, then you can read those values as individual params by destructuring:
const takesThreeParameters = ([a, b, c]) => console.log(`a:${a} b:${b} c:${c}`)
const p = new Promise(res => setTimeout(res([1, 2, 3]), 2000))
p.then(takesThreeParameters)
EDIT: As per @Naor Tedgi 's answer, if you want to avoid to modify your existing functions, you can wrap them into an HOF:
const wrap = (cb) => (args) => cb(...args)
const takesThreeParameters = (a, b, c) => console.log(`a:${a} b:${b} c:${c}`)
const wrappedFn = wrap(takesThreeParameters)
const p = new Promise(res => setTimeout(res([1, 2, 3]), 2000))
p.then(wrappedFn)
CodePudding user response:
No, the Promises spec only defines the first parameter. You can't pass in others, you can only emulate it using destructuring or spreads.
From the Promises/A spec, 2.2.2.1, emphasis mine:
If
onFulfilled
is a function:
- it must be called after
promise
is fulfilled, withpromise
’s value as its first argument.
The ES6 spec describes this in NewPromiseReactionJob (27.2.2.1) step 1.e:
e. Else, let handlerResult be Completion(HostCallJobCallback(handler, undefined, « argument »)).
In both cases the spec allows a single Promise handler value. Unlike features like setTimeout
where additional arguments can be passed to the handler, there is no such option for Promises.
You can at least avoid repeating the argument list with spread syntax:
Promise.resolve()
.then(providesThreeValues)
.then(threeValues => takesThreeParameters(...threeValues))
.then(console.log);
Likewise, if you are willing to edit the function, the edit to takesThreeParameters
could be minimal with array destructuring:
function takesThreeParameters ([a, b, c]) { // new brackets
console.log(`${a} ${b} ${c}`);
return 'something';
}
CodePudding user response:
this hack should solve all use cases use the spreadParams function and resolve array
function spreadParams(cb) {
return function (args) {
return cb(...args)
}
}
const takesThreeParameters = spreadParams((a, b, c) => {
console.log(`${a} ${b} ${c}`);
return 'something';
})
new Promise((resolve, reject) => resolve([1, 2, 3]))
.then(spreadParams(takesThreeParameters))
.then(console.log);