The problem is I have a tasklist of APIs but the issue is I can't hit more than 3 APIs at once simultaneously from my endpoint to the server(it's the server restriction that it will not respond back to the same requested URL which has more than 3 API called in processing). So I have found a way around to achieve this task but there is a better approach that I know but have not any clue about that how to do but i found a simple solution which is sequentially hit API and wait for the response then hit another but this takes too much time
More optimized solution if N = 3, then I will execute 'task1', 'task2', and 'task3' in the taskList concurrently. Once task2 finishes, you then execute 'task4', and so on. This is the solution that I want to achieve without using any library.
const taskList = [
"task1",
"task2",
"task3",
"task4",
"task5",
"task6",
"task7",
"task8",
"task9",
"task10",
];
const dummyAPICalled = (task) => {
console.log(task, "started");
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log(task, "ended");
resolve(true);
}, Math.random() * 20000);
});
};
Hit one by one API's called and wait for the responses.
for await (const task of taskList) {
await dummyAPICalled(task);
}
Hit all APIs at once parallel but didn't get a response because of server requirement
const promises = taskList.map((task) => dummyAPICalled(task));