Skip to content

Instantly share code, notes, and snippets.

@getify
Created June 2, 2018 04:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save getify/cb8079dcd11e1d2080bf3f8036aee2b3 to your computer and use it in GitHub Desktop.
Save getify/cb8079dcd11e1d2080bf3f8036aee2b3 to your computer and use it in GitHub Desktop.
waitForEvent(..) -- promisified event listener
function waitForEvent(elem,evtName) {
return new Promise(function c(resolve){
elem.addEventListener(evtName,function onEvent(evt){
elem.removeEventListener(evtName,onEvent,false);
resolve(evt);
},false);
});
}
@VivekNayyar
Copy link

Why inside a promise?

@IntpLLC
Copy link

IntpLLC commented Jun 2, 2018

This may be to free up the call stack in order to have a smoother UI

@third774
Copy link

third774 commented Jun 2, 2018

Would be useful for using with async/await

@GbalsaC
Copy link

GbalsaC commented Jun 2, 2018

Don't use async/await on this...

@LoicMahieu
Copy link

Sindre already did it :)
https://github.com/sindresorhus/p-event

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment