1

I'm trying to call the same this.func twice asynchronously, this.func makes a http request but I can see through the network tab that these two functions are not running in parallel. I've already read these answers: answer 1, answer 2

    async componentDidMount() {     
   //...
    var applicationVersion2 = await Promise.all([this.func(this.state.applicationUrl), this.func(this.state.applicationUrl2)]); 
   //...
    }

how it is now:

enter image description here

how it should be:

enter image description here

Eduardo
  • 99
  • 2
  • 11
  • 1
    There is only a limited number of workers, that might explain why not all 14 calls run in parallel. – eckes May 21 '18 at 21:43
  • 3
    Not to state the obvious here but are you sure that `this.func` is async and/or returns a promise? The top graph is exactly what I would expect from two non-async functions run in series. – Jay Allen May 21 '18 at 21:46
  • It will probably help if you show what this. func does. – HMR May 21 '18 at 22:00

1 Answers1

1

As I mentioned in the comments above, I suspect that this.func isn't asynchronous code and/or returning a promise although without seeing it, it's hard to say for sure. Try this:

async componentDidMount() {     
  //...
  const first = this.func(this.state.applicationUrl)
  console.log(`First: ${first}`)   
  const second = this.func(this.state.applicationUrl2)
  console.log(`Second: ${second}`) 
  const applicationVersion2 = [await first, await second]
  //...
}

In your console.log, you should see:

First: Promise {<pending>}
Second: Promise {<pending>}

But I'm guessing you'll see a non-promise value from both. If that's the case, make this.func an async function or return a Promise from it.

Jay Allen
  • 465
  • 3
  • 8
  • I tried and it saw `First: [object Promise]` on console but on the network tab it looks the same of my first print. Here is a [print](https://imgur.com/V8E3MFc) of the code (some funcs and vars are wirh different names). If you can't reproduce my problem, let me know I will share the full code of the project – Eduardo May 22 '18 at 16:33
  • @Eduardo Actually, it would be helpful if you could add the code (copy and paste) of those two functions to your original post. – Jay Allen May 22 '18 at 17:42
  • Nevermind. See here: https://gist.github.com/jayallen/746f122d70301502dcd0ad5bb46015cc That code definitely loads the resources concurrently. I'm not sure what the issue if with your code. :-/ – Jay Allen May 22 '18 at 18:29
  • I tried your code on new project it worked, I will search on my code what's wrong and when I find I'll post here, thank you for your help – Eduardo May 24 '18 at 18:42
  • @Eduardo I’m glad to hear it and hope that it helps! – Jay Allen May 24 '18 at 20:05