Task Queue
Queuing
TaskQueueMutable is a utility class to run a series of functions one-by-one.
‘Tasks’ are just async functions. The queue waits for the function to return a result, after which it takes the next item in the queue to process.
It’s also possible to listen for when the queue has started processing some events and when it has finished, with the ‘started’ and ‘empty’ events.
Calling with a timeout
There are times you might want to call a function, but handle cases where it fails or we don’t get a result after some period. Flow.waitFor helps with this.
The signature is:
Flow.waitFor returns a function for signalling the result. If this function is not called within the timeout, or is passed an error message, the abort callback will run.
In the above example, we only listen for the error case - if the timeout happens or the user presses a key. We can also get notified when the operation completes, successfully or not.