Helper function for calling code that should fail after a timeout.
In short, it allows you to signal when the function succeeded, to cancel it, or
to be notified if it was canceled or completes.
It does not execute or track the outcome of execution itself. Rather it's a bit
of machinery that needs to be steered by your own logic.
waitFor takes a timeout, and two lifecycle functions, onAborted and onComplete.
onAborted is called if the timeout has elapsed. onComplete will run on either success or failure.
try { runSomethingThatMightScrewUp(); done(); // Signal it succeeded } catch (e) { done(e); // Signal there was an error }
Example: Verbose
// This function is called by `waitFor` if it was cancelled constonAborted = (reason:string) => { // 'reason' is a string describing why it has aborted. // ie: due to timeout or because done() was called with an error };
// This function is called by `waitFor` if it completed constonComplete = (success:boolean) => { // Called if we were aborted or finished succesfully. // onComplete will be called after onAborted, if it was an error case }
// If done() is not called after 1000, onAborted will be called // if done() is called or there was a timeout, onComplete is called constdone = waitFor(1000, onAborted, onComplete);
// Signal completed successfully (thus calling onComplete(true)) done();
// Signal there was an error (thus calling onAborted and onComplete(false)) done(`Some error`);
The completion handler is useful for removing event handlers.
Helper function for calling code that should fail after a timeout. In short, it allows you to signal when the function succeeded, to cancel it, or to be notified if it was canceled or completes.
It does not execute or track the outcome of execution itself. Rather it's a bit of machinery that needs to be steered by your own logic.
waitFor
takes a timeout, and two lifecycle functions,onAborted
andonComplete
.onAborted
is called if the timeout has elapsed.onComplete
will run on either success or failure.When calling
waitFor
you get back a function to signal success or failure:Example: Compact
Example: Verbose
The completion handler is useful for removing event handlers.