Run promise requests sequentially and return results in order
You are tasked with creating an asynchronous request queue in JavaScript. The primary objective is to ensure that each request in the queue is executed sequentially, adhering to the order in which they were added to the queue. The execution of a subsequent request should commence only after the completion of the preceding one. Your challenge involves developing a function named requestQueue, which accepts an array of request promises as input and returns a promise that resolves to an array containing the results of these requests, maintaining the sequence of their execution. An asynchronous request queue builds on Promises.
Consider the following example of requests:
const requests = [
fetch('https://api.example.com/data/1'),
fetch('https://api.example.com/data/2'),
fetch('https://api.example.com/data/3')
];
The expected output should be an array containing the results of the requests, in the order they were executed:
[
// Result of the first request
{ /* data for request 1 */ },
// Result of the second request
{ /* data for request 2 */ },
// Result of the third request
{ /* data for request 3 */ }
]
The requestQueue function is crucial for scenarios where the order of request execution and completion is paramount, ensuring that each request is given due consideration in a sequential manner.