Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
wait for function execution to end in array.map before next iteration
const handleEnter = (e) => {
        if (e.key === 'Enter') {
            const bulkSearchString = e.target.value;
            const jobIdArray = bulkSearchString.split(',').map((value) => value.trim());
            jobIdArray.map((jobId, i) => fetchEachJob(jobId, atsFilter.type)
                .then(result => {
                    if (result.length) {
                        const value = result[0];
                        const index = findIndex(formValuesId, (o) => o === value.id);
                        if (index < 0) {
                            setTimeout(
                                () => {
                                    addNewOption(
                                        value,
                                        atsFilter.filterType,
                                        atsFilter.type,
                                        atsFilter.radio,
                                        true
                                    );
                                },
                                500 * i
                            );
                        }
                    }
                }));
            e.preventDefault();
        }
    };

fetchEachJob: API call

addNewOption: Synchronous function

In the above code, I have added setTimeout so that addNewOption execution completes before it is called with new parameters. Without the setTimeout, the addNewOption is getting cancelled by the next call, thus returning result for only the last call. The code works fine with setTimeout but it is not the optimal solution.

Is there a way to wait for this synchronous function to finish before it is called by the next iteration?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!