-2

new Promise((resolve, reject) => {
    resolve('result1', 'resolve2');
}).then((result1, result2) => {
    console.info(result1, result2);
});

Why Promise returns only one result in JavaScript like in the above code?

I'm sorry for my unclearly describe. I'm know how to return an object or an array value in resolve. I'm just curious why Promise design to return only on result.

zsk
  • 7
  • 1
  • 2
    Resolve only takes one argument & that is the returned data. Just resolve an array with your two results - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – Jackthomson Sep 29 '17 at 07:23
  • because it take only one parameter, use an array : `resolve('result1', 'resolve2');` ... `then((array)=>{result1 = array[0];result2=array[1];});` – Walfrat Sep 29 '17 at 07:23
  • 2
    Possible duplicate of [Can promises have multiple arguments to onFulfilled?](https://stackoverflow.com/questions/22773920/can-promises-have-multiple-arguments-to-onfulfilled) – SBylemans Sep 29 '17 at 07:26
  • I'm just curious **why** Promise design to return only one result. – zsk Oct 13 '17 at 07:38

1 Answers1

1

Promise always returns one value. But you can do a trick to get as two using Array destructing. Pass an array or object to the resolve/reject function and using destructing get them as separate variables.

new Promise((resolve, reject) => {
    resolve( ['result1', 'resolve2'] );
}).then( ( [result1, result2] ) => {
    console.info(result1, result2);
});
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112