I'm using babeljs with es7 style async/await methods. I have a main script that will call a async method on an array of objects that all return promises. I use Promise.all() to wait for all of those to return, however, these tasks could take a long time and if they exceed a threshold I would like to abort all of them, and the task handle that in an appropriate way.
Is there anyway to accomplish such a thing? Currently the only way that I can think of is by spawning a process that does the work of calling these methods and waiting for them to all resolve, and if the time limit is reach, it can kill the process and do whatever handling it needs.
Update: Some clarification about these methods that the main script is waiting on... They might be doing a long series of operations (calling external systems, streaming files somewhere, etc) and not performing one single action that could be canceled independently.
Update #2: Some untested semi-psuedo code
class Foo1 {
async doSomething() {
// call some external system
// copy some files
// put those files somewhere else (s3)
}
}
class Foo2 {
async doSomething() {
// Do some long computations
// Update some systems
}
}
class FooHandler {
constructor() {
this.fooList = [];
}
async start() {
await Promise.all(this.fooList.map(async (foo) => {
return await foo.doSomething();
}));
}
}
let handler = new FooHandler();
handler.fooList.push(new Foo1());
handler.fooList.push(new Foo2());
// if this call takes too long because of slow connections, errors, whatever,
// abort start(), handle it in whatever meaningful way, and continue on.
await handler.start();