GithubHelp home page GithubHelp logo

event-wait-until's Introduction

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

event-wait-until's People

Contributors

matthewp avatar

Stargazers

Gustavo Giserman avatar Eric Clemmons avatar  avatar Colin Gourlay avatar

Watchers

 avatar James Cloos avatar  avatar

event-wait-until's Issues

API

The goal of this repo is to implement a ponyfill for this issue.

What I want is to be able to do something like:

button.addEventListener('click', ev => {
  waitUntil(ev)(new Promise(resolve => {
    setTimeout(function(){
      resolve();
    }, 50);
  }));
});

To implement we need to:

  1. preventDefault and stopPropagation on the event, to stop it.
  2. When the promise is resolved, retrigger the event (might have to clone it).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.