event-wait-until

1.0.0 • Public • Published

Build Status

event-wait-until

Prevent an event from continuing to propagate up (or down) the down until a Promise has resolved. This is good if you need to do some asynchronous activity before determining if the event should be stopped or prevented.

This is a ponyfill for this WICG issue.

Install

With yarn:

yarn add event-wait-until

Example

Here's a simple, and silly, example where we query a Web Worker to determine if the event should be prevented.

anchor.addEventListener('click', ev => {
  eventWaitUntil(ev)(new Promise(resolve => {
 
    worker.postMessage({question: 'Should this be prevented?'});
 
    worker.onmessage = function(msg){
      const reply = msg.data.reply;
      if(reply === 'prevent') {
        ev.preventDefault();
      }
      resolve();
    };
 
  }));
});

API

event-wait-until is a function that is placed on the window object as eventWaitUntil. The signature is eventWaitUntil(event) -> fn(promise). See the above example.

License

BSD 2 Clause

Dependents (0)

Package Sidebar

Install

npm i event-wait-until

Weekly Downloads

1

Version

1.0.0

License

BSD-2-Clause

Last publish

Collaborators

  • matthewp