0

I have a react application interacting with a drop wizard REST API. In my code I noticed the following odd bug.

In attempting to set the values of two variable and then use them to set state, I had the following

const [inventorRes, sellerRes] =
await Promise.all([
    LoginTransport.getAllUsers('Inventory Owner'),
    LoginTransport.getAllUsers('Seller'),
]);

where LoginTransport is a method that calls the external API. Now I noticed that in doing this, both inventor res and sellerRes would be set to the value of LoginTransport.getAllUsers('Inventory Owner') as opposed to the respective arguments.

When I break it up into:

const inventorRes = await LoginTransport.getAllUsers('Inventory Owner');
const sellerRes = await LoginTransport.getAllUsers('Seller');

The behavior is correct.

My understanding from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all was that Promise.all returned back an iterable. Is not the case that javascript would attempt to match the values on the left-hand iterable with the right hand? And assuming that's true but I insisted on using Promise.all() how could I accomplish the task I want to do?

Sidharth Ghoshal
  • 658
  • 9
  • 31
  • 1
    a dumbed down version of your code runs just fine - https://jsfiddle.net/pjh7djx8/ - perhaps your `LoginTransport.getAllUsers` function is broken in some way where running it twice conceptually "at the same time" breaks it – Jaromanda X Feb 13 '17 at 04:47
  • 3
    The behavior you're describing shouldn't be happening. You should check whether `LoginTransport.getAllUsers` has some side-effect that causes it to return the same value if it's called in quick succession. You could check it like this: `const p1 = LoginTransport.getAllUsers('Inventory Owner'); const p2 = LoginTransport.getAllUsers('Seller'); const inventorRes = await p1; const sellerRes = await p2;` – JLRishe Feb 13 '17 at 04:48

1 Answers1

1

Now I noticed that in doing this, both inventor res and sellerRes would be set to the value of LoginTransport.getAllUsers('Inventory Owner') as opposed to the respective arguments.

That would be the fault of LoginTransport.getAllUsers. Promise.all does what you are expecting it to do.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375