0
const filesToTranslate = await getFilesToTranslate();
await Promise.all(filesToTranslate.map(async item => {
  doExtraction(item)
}));

I would like to execute doExtraction every 30 seconds so the server don't choke.

Any smart to do that?

2
  • This will execute doExtraction() serially, every 30 seconds a single doExtraction(). Is that what you need? Commented Jun 11, 2020 at 8:50
  • @artfulbeest Yes thats correct. Commented Jun 11, 2020 at 9:00

1 Answer 1

2

You could use a timeout:

const filesToTranslate = await getFilesToTranslate();
for (const item of filesToTranslate) {
    await new Promise(resolve => setTimeout(resolve, 30000));
    doExtraction(item);
}

Using await inside the loop will pause the iteration until the Promise has resolved.


If you want the first doExtraction to execute immediately:

const filesToTranslate = await getFilesToTranslate();
for (const item of filesToTranslate) {
    doExtraction(item);
    if (filesToTranslate.Slice(-1) !== item) {
        await new Promise(resolve => setTimeout(resolve, 30000));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

await won't without parent async function, In order to use await you should always have an async function.
@kamal The parent function must be async; await is being used for getFilesToTranslate().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.