0

In the browser's console I am expecting 3 consecutive results which are: Bears, Lions, Tigers. in this order. What I get is only Bears

new Promise((resolve, reject) => {
    return reject(new Error('no bears'));
 setTimeout(() => {
         resolve('Bears', 'Lions', 'Tigers')
   }, 1000);
})

.then(quote => {
    console.log(quote);
})
Harshal Patil
  • 17,838
  • 14
  • 60
  • 126
Sarkis
  • 93
  • 2
  • 9

2 Answers2

2

Only the first argument to resolve() is actually passed to the then handler. You probably want to resolve with an array instead

resolve(['Bears', 'Lions', 'Tigers'])

Also, you have a return reject() before your setTimeout call so your setTimeout code is unreachable and it never happens.

Chirag Ravindra
  • 4,760
  • 1
  • 24
  • 35
  • yea I tried that but Gives out an array instead of logs. works but ive seen it work without the [ ] im 100% sure. could it be a version difference? – Sarkis Feb 05 '19 at 10:36
  • I'm not entirely sure but I'm pretty certain that `resolve` accepts only one argument. The [MDN reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve) also says as much. With respect to getting an array back, what's the problem if it is an array? Array/Object is usually how you pass around multiple items in a single value in JS – Chirag Ravindra Feb 05 '19 at 10:38
0

You can only send a single value, consider sending as an array and spreading it at the reciever

new Promise((resolve, reject) => {
    return reject(new Error('no bears'));
    setTimeout(() => {
         resolve(['Bears', 'Lions', 'Tigers'])
   }, 1000);
})
.then(quote => {
    console.log(...quote); // spreads to 3 items
})
Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25