How do you properly return multiple values from a promise?

后端 未结 9 1387
慢半拍i
慢半拍i 2020-12-04 14:14

I\'ve recently run into a certain situation a couple of times, which I didn\'t know how to solve properly. Assume the following code:

somethingAsync()
  .the         


        
9条回答
  •  离开以前
    2020-12-04 14:38

    You can't resolve a promise with multiple properties just like you can't return multiple values from a function. A promise conceptually represents a value over time so while you can represent composite values you can't put multiple values in a promise.

    A promise inherently resolves with a single value - this is part of how Q works, how the Promises/A+ spec works and how the abstraction works.

    The closest you can get is use Q.spread and return arrays or use ES6 destructuring if it's supported or you're willing to use a transpilation tool like BabelJS.

    As for passing context down a promise chain please refer to Bergi's excellent canonical on that.

提交回复
热议问题