You can use Array.prototype.shift()
, .then()
to schedule call to a function if array containing arrays .length
evaluates to true
, else return array of responses. Which performs the process in sequence.
const requests = [[..], [..], [..]];
let results = [];
let request = requests.shift();
let handleRequests = (data) => fetch("/path/to/server", {
method:"POST", body:data
})
.then(response => response.text())
.then(res => {
results.push(res);
return requests.length ? handleRequest(requests.shift()) : results
});
handleRequest(request)
.then(res => console.log(res)
, (err) => console.log(err));
Note, if order is not a part of requirement, you can substitute Promise.all()
, Array.prototype.map()
for .shift()
.
let handleRequests = (data) => fetch("/path/to/server", {
method:"POST", body:data
})
.then(response => response.text());
Promise.all(requests.map(handleRequests))
.then(res => console.log(res)
, (err) => console.log(err));