I know that very soon I will have to make the same query in more than 20 databases and therefore I’m trying to find a solution to rewrite my code and make it clearer and more efficient.
I tried to use the Javascript Logical Operator “Or” || to simplify the code but it doesn’t seem to work.
@russian-dima It’s pure javaScript. Not something that’s special for Velo.
So it’ll be documented here.
Basically you run several promises in parallel and once they’re resolved you receive an array of the results [res1, res1].
Since the queries here are independent there’s no point in waiting for one of them to get resolved before you run the other, so Promise.all() is ideal for this case.
@russian-dima You’re welcome. Promise.all() is a strong tool. There’s also Promise.race() that runs several promises in parallel and fulfills or rejects once one of them gets resolved or rejected. This is also useful in some cases.
Anyway when you use Promise.all() keep in mind that if there’re too many promises with too many results it might fail due to timeout, In that case chain the promises.
I’ll be using the last solution from @jonatandor35 as it’s even more compact than what I expected ^^
I learned Promise.all but never used it before. That’s a first for me and a good use case Thanks
The solution from @russian-dima was interesting too. I’ll keep it for another problem I might encounter. Thanks !