I am creating a script in node.js (V8.1.3) that looks at similar JSON data from several APIs and compares the values. More precisely, I look at different market prices of different stocks (actually cryptocurrencies).
I am currently using promise.all to wait for all responses from the corresponding APIs.
let fetchedJSON = await Promise.all([getJSON(settings1), getJSON(settings2), getJSON(settings3) ... ]);
However, Promise.all throws an error if at least one promise is rejected with an error. There is a Promise.some function in the Bluebird documentation, and that is almost what I want. As I understand it, it accepts an array of promises and resolves the two fastest promises that are resolved, or otherwise (if less than two promises are resolved) gives an error.
The problem is that, firstly, I do not want the two fastest promises to be resolved as returnable, I want any successful promises to be returned if there are more than 2. This seems to be what Promise.any does, except that the minute counter is 1. (I need a minimum score of 2)
Secondly, I do not know how many promises I will expect (in other words, I do not know how many APIs I will request data from ). It can only be 2 or 30. It depends on user input.
Now, when I write this, it seems to me that there is simply a way to get a promise. Anyone with a score of 2, and that would be the easiest solution. Is it possible?
By the way, not sure if the headline really summarizes the question. Please suggest changing the name :)
UPDATE: Another way I can write a script is that the first two downloadable APIs start to be computed and passed to the browser, and then every next JSON that is loaded and computed after it. Thus, I do not wait for all Promises to be fulfilled before I begin to calculate the data and transfer the results to the external interface. Is this possible with a function that also works for other circumstances?
What I mean looks something like this:
JSON request in parallel ...
| ----- JSON1 ------ |
| --- JSON-FAILS --- | > catch an error> do something with an error. Does not affect the following results.
| ------- JSON2 ------- | > Matches at least 2 results> calculates JSON> for the browser.
| ------- JSON3 --------- | > computes JSON> for the browser.