Created
August 20, 2020 10:46
-
-
Save gilad905/0b7d9073e075b7ed15ffec4f197cb572 to your computer and use it in GitHub Desktop.
eventOrTimeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function waitFor(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
function eventOrTimeout() { | |
let [evResolve, evReject] = []; | |
const evPromise = new Promise((res, rej) => [evResolve, evReject] = [res, rej]); | |
someElement.on('someEvent', function(event) { | |
if (event.someCondition) | |
evResolve(event.someValue); | |
else if (event.otherCondition) | |
evReject(); | |
}); | |
const timeout = 15000; | |
return Promise.race([evPromise, waitFor(timeout)]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment