eventOrTimeout

Gilad 905
2020-08-20T10:46:31Z

gistfile1.txt

| 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)]);
| }