Skip to content
Advertisement

Resolving a promise to multiple parameter values

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?).

Advertisement

Answer

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, with promise’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';
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement