2

Both Axios and Fetch return a promise and logically their callback should go into the promise microtask queue. But both of them are doing network I/O, doesn't that mean their callback should go into the I/O queue in the event loop? To which queue do their callbacks go into?

axios.get('/url')
     .then(data => console.log(data)) // this callback goes into the microtask queue or I/O queue?
kjunz
  • 111
  • 1
  • 7
  • They use callbacks (or native tasks) internally, which are network event handlers, that resolve the promise. – Bergi Jun 20 '21 at 21:12
  • are you saying that when the network request is completed, then the callback will get added to the I/O queue instead of the promise microtask queue? – kjunz Jun 20 '21 at 21:15
  • The callback that resolves the promise, yes - but this might as well be an implementation detail. The handlers that are attached to the promise do then of course go into the microtask queue. – Bergi Jun 20 '21 at 21:19
  • @Bergi ahh okay yea that make sense. When it runs axios.get() the inner http api will make a request, then data comes back and triggers the http api callback which then get added into the IO queue, then we resolve that data back to the axios.get().then(data => data), then this obviously gets added into the microtask queue. Can you confirm this logic? – kjunz Jun 20 '21 at 21:34
  • 1
    https://stackoverflow.com/questions/62566559/what-does-fetch-do-with-event-loop-in-browser/62566665#62566665 about the same happens in node. – Kaiido Jun 21 '21 at 02:26
  • @Kaiido hmm...but isn't a network request an I/O operation as well? according to your answer the network request happens outside of the event loop, and its http callback won't be added into the I/O queue. The second part I can understand as fetch/axios resolves the data then obviously it goes into the promise microtask queue. – kjunz Jun 21 '21 at 02:37
  • "and its http callback won't be added into the I/O queue" where did you read that in my answer there? The point is exactly that "the browser will queue a new task". – Kaiido Jun 21 '21 at 02:39
  • my bad I was thinking about something different lol. Your answer makes perfect sense now. thanks for the linking that here. – kjunz Jun 21 '21 at 03:08

0 Answers0