I am building an Angular app. In the constructor of my component, I am initiating a request for several elements from the server, which naturally occur asynchronously. Those requests are returned to me as rxjs/Behavior
objects, to which I am subscribed.
What I need to do is continue processing as soon as the last one is back. But I NEED to make sure that they are processed in the order in which they were submitted, not necessarily in the order in which they were returned.
I could create a janky hack to detect when the last one has returned:
let counter = 0;
let output = new Array( input.length );
for ( let i = 0 ; i < input.length ; i++ ) {
counter++;
fetch( input[i] ).subscribe(
result => {
output[i] = result;
counter--;
if ( counter === 0 ) {
// what to do at the end
}
}
);
}
And this works. But it's ugly, it is not easy to understand, and not what I would call production-ready code.
What is the Angular way of doing something once an array of subjects have all been fulfilled?