Mixin: EventPromise

EventPromise

Creates promises from standard JavaScript events for inline asynchronous execution. This class binds itself to the global object prototype and is therefore available to all derived objects in the current execution context.

This global class requires ECMAScript 2017 support.

Mixes In:
  • Object.prototype
Source:

Methods

(static) onEventPromise(eventType, useCapture)

Registers an event listener on a target object and returns a Promise.
Parameters:
Name Type Default Description
eventType String The type of event to register. This can match any event type dispatched by the target object.
useCapture Boolean false As in standard events, this parameter specifies if the event is dispatched in the capture phase (true), or in the bubble phase (false).
Source:
Returns:
A standard Promise object that will receive either a resolution when the event fires or a rejection if the event could not be registered.
Examples
//all objects in the current execution context will inherit the
//onEventPromise handler automatically
window.onEventPromise("click").then(
  function(resolve, reject) {
     alert ("Clicked on window");
  }
)
//the returned Promise also works with async functions as expected
async function doOnClick() {
 event = await window.onEventPromise("click");
 alert ("Clicked on window");
}
doOnClick();