start running `time += 1` 1ms after `const response = await fetch(`/summarize?url=${encodeuricomponent(text)}`)` starts fetching, and stop incrementing immediately after it ends in javascript

You can achieve the desired behavior using Promise and setTimeout functions as follows:

index.tsx
let time = 0;
const responsePromise = fetch(`/summarize?url=${encodeURIComponent(text)}`);
const timeoutPromise = new Promise(resolve => setTimeout(resolve, 1));

Promise.all([responsePromise, timeoutPromise])
  .then(() => clearInterval(intervalId));

const intervalId = setInterval(() => {
  time++;
}, 1);
309 chars
11 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