1
const getFilesList = async () => {
 await //first api call
 wait for 1 second
 await //second API call
};

I want to wait for one second between the first and second API as a result of the second API being dependent on the first API and that result takes some time to generate.

  • 1
    `wait for 1 second` ...easy ... `await new Promise(r => setTimeout(r, 1000))` - or `const wait = ms => new Promise(r => setTimeout(r, ms))` ... then you can `await wait(1000)` – Bravo Feb 22 '22 at 06:43
  • 1
    If the second API is dependant on the first, then that's the job of `await`. I don't believe waiting for a certain amout of time is a good idea. – Moaaz Bhnas Feb 22 '22 at 06:45
  • 1
    This is [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - if `await //first api call` does not actually wait for the call to be finished *then you shouldn't wait for a random time*. You should instead fix that call so it resolves when it is supposed to, not prematurely. – VLAZ Feb 22 '22 at 06:50
  • @MoaazBhnas the second API takes some time to generate the result, if called immediately it will give response 204 with no data. –  Feb 22 '22 at 06:51
  • Doesn't the second API wait for the first to finish? – Moaaz Bhnas Feb 22 '22 at 06:58
  • 1
    @MoaazBhnas the first API gives and id as a response and we use that in the second API to get the result but if sent instantly it will result in 204 as the second API is using some database to generate the result –  Feb 22 '22 at 07:06

1 Answers1

0

you can use timers in JS or wait.

Like this :

const getFilesList = async () => {
 await //first api call
 await new Promise(resolve => setTimeout(resolve, 1000))
 await //second API call
};