Skip to content

Instantly share code, notes, and snippets.

@gosoccerboy5
Last active October 19, 2021 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gosoccerboy5/ac4c51f4f045564528e1076fd01f536d to your computer and use it in GitHub Desktop.
Save gosoccerboy5/ac4c51f4f045564528e1076fd01f536d to your computer and use it in GitHub Desktop.
Await the next event occurring on an element.
// Use this to await the next event occurring on an element.
/* Usage example:
await querySelector("#myButton").next("click");
print("Button clicked!");
*/
import "dart:html";
import "dart:async";
extension on Element {
Future<Event> next(String name) {
final completer = Completer<Event>();
void handler(Event event) {
removeEventListener(name, handler);
completer.complete(event);
}
addEventListener(name, handler);
return completer.future;
}
}
void main() {}
// Use this to await the next event occurring on an element.
/* Usage example:
await document.querySelector("#myButton").next("click");
alert("Button clicked!");
*/
HTMLElement.prototype.next = function(eventName) {
return new Promise(function(resolve, reject) {
var handler = function(event) {
resolve(event);
this.removeEventListener(eventName, handler);
};
this.addEventListener(eventName, handler);
}.bind(this));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment