最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the best way to return multiple values in a promise chain - Stack Overflow

programmeradmin7浏览0评论

I do realise that when returning a non-promise in a .then() handler, it is immediately passed on to the next handler, if it's a promise that is returned, executing is halted for the promise to resolve before it is passed on to the next handler.

Also I know that only one value can be returned from a promise.

That beeing said, how would I go about returning multiple parameters from one .then() handler to the next one? Esepcailly if it's a mix of promises and non-promises. Currently I put everything into a custom object, return it, and use async await in the following then() handler for the promises to reslolve.

Then is use the resolved promise values and the non-promise value to do some work together.

This works fine but my gut is saying that this is somehow not the way it is supposed to be... maybe?

Example:

const current = 'blah';
const previous = 'blubb';

this.doSomeAsyncWork()
.then(
    result => {
        const nonPromiseValue = new domSomethingSynchronous(current, previous);
        // "custom object, mix of promises and non-promises"
        return {
            nonPromise: nonPromise,
            promiseA: ControllerA.asyncOperationA(current, nonPromiseValue.someProperty),
            promiseB: ControllerB.asyncOperationB(nonPromiseValue.someOtherProperty),
        }
    }
)
.then(
    async x => {
        const nonPromiseValue = x.nonPromiseValue;
        const valueA = await x.promiseA;
        const valueB = await x.promiseB;

        // do something with the results of those three variables

    }
)
.catch(
    // ...
)

I do realise that when returning a non-promise in a .then() handler, it is immediately passed on to the next handler, if it's a promise that is returned, executing is halted for the promise to resolve before it is passed on to the next handler.

Also I know that only one value can be returned from a promise.

That beeing said, how would I go about returning multiple parameters from one .then() handler to the next one? Esepcailly if it's a mix of promises and non-promises. Currently I put everything into a custom object, return it, and use async await in the following then() handler for the promises to reslolve.

Then is use the resolved promise values and the non-promise value to do some work together.

This works fine but my gut is saying that this is somehow not the way it is supposed to be... maybe?

Example:

const current = 'blah';
const previous = 'blubb';

this.doSomeAsyncWork()
.then(
    result => {
        const nonPromiseValue = new domSomethingSynchronous(current, previous);
        // "custom object, mix of promises and non-promises"
        return {
            nonPromise: nonPromise,
            promiseA: ControllerA.asyncOperationA(current, nonPromiseValue.someProperty),
            promiseB: ControllerB.asyncOperationB(nonPromiseValue.someOtherProperty),
        }
    }
)
.then(
    async x => {
        const nonPromiseValue = x.nonPromiseValue;
        const valueA = await x.promiseA;
        const valueB = await x.promiseB;

        // do something with the results of those three variables

    }
)
.catch(
    // ...
)
Share Improve this question edited Sep 6, 2022 at 10:32 Sebastián Palma 33.5k6 gold badges44 silver badges63 bronze badges asked Aug 16, 2018 at 5:45 baoussbaouss 1,9402 gold badges39 silver badges71 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Use return Promise.all on the array of Promises and non-Promises at the end of a .then, and then you can destructure the results immediately in the next .then, no await nor async needed.

The Promise.all will resolve once all Promises in the array have resolved. The non-Promises passed to it will just be passed to the next .then.

const makeProm = () => new Promise(resolve => setTimeout(resolve, 1000, 'resolveValue'));

Promise.resolve()
  .then(() => {
    const prom = makeProm();
    const otherValue = 'foo';
    return Promise.all([prom, otherValue]);
  })
  .then(([resolveValue, otherValue]) => {
    console.log(resolveValue, otherValue);
  });

发布评论

评论列表(0)

  1. 暂无评论