You can achieve the desired behavior using Promise
and setTimeout
functions as follows:
index.tsx309 chars11 lines
Here, we create a Promise
(responsePromise
) for our fetch
request which starts fetching immediately when the code runs. We also create another Promise
(timeoutPromise
) which resolves after 1ms. We use Promise.all
to wait for both promises to resolve, and then we clear the interval using clearInterval
function.
Additionally, we use setInterval
function to increment time
variable every 1ms until it's cleared inside the Promise
chain.
gistlibby LogSnag