Saturday, April 10, 2021

Conditional Promise.all

Have been in a situation where you needed to dynamically shoot out HTTP requests based on some branching logic? Are you using a Promise based HTTP client library like fetch or axios?

Fear not, here is a solution for your needs!

In general the solution is based on coming up with an interface that's not only returning the data when the Promise was resolved, but also give a companion flag that identifies the promise it was initiated from.


interface DynamicPromiseResult {
data: unknown;
promise: string;
}
const promises: Array<Promise<DynamicPromiseResult>> = []
if (true) { // some logic
const promise1 = fetch('https://jsonplaceholder.typicode.com/posts/1').then(data => ({ data, promise: 'promise1' }));
promises.push(promise1);
}
if (false) { // some logic
const promise2 = fetch('https://jsonplaceholder.typicode.com/posts/2').then(data => ({ data, promise: 'promise2' }));
promises.push(promise2);
}
const results = await Promise.all(promises)
results.forEach(result => {
// pattern matching would be lovely here, but JS/TS does not support it yet
switch (result.promise) {
case 'promise1':
doSomething();
break;
case 'promise2':
doSomething2();
break;
default:
throw new Error('Resulting promise not found');
}
});

You can find the idea on this SO post as well.

No comments:

Post a Comment