How to pass multiple values to resolve function in Promise

In Promise, you can pass an argument to resolve function and use in then function. If you want to pass multiple values, you should be careful.

new Promise(resolve => {
  resolve(1, 2)  // passes two values
}).then((result1, result2) => {
  console.log(result1)  // 1
  console.log(result2)  // undefined
})

In then function, I couldn’t get the second argument though I passed 2 arguments to resolve function. Why not? Because resolve function can be passed only one argument.

Then, we have to use array or plain object to pass multiple values.

new Promise(resolve => {
  resolve([1, 2])  // gather values into an array
}).then(result => {
  console.log(result[0])  // 1
  console.log(result[1])  // 2
})

Yes, we can pass multiple values to then function. It may be easy if you know it, but I guess it is a bit hard to notice when you are getting stuck.