mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
test: refactor utils.waitForEvents into utils.waitEvent (#2336)
This simplifies waiting for events in tests.
This commit is contained in:
@@ -76,22 +76,10 @@ const utils = module.exports = {
|
||||
/**
|
||||
* @param {!EventEmitter} emitter
|
||||
* @param {string} eventName
|
||||
* @param {number=} eventCount
|
||||
* @return {!Promise<!Object>}
|
||||
*/
|
||||
waitForEvents: function(emitter, eventName, eventCount = 1) {
|
||||
let fulfill;
|
||||
const promise = new Promise(x => fulfill = x);
|
||||
emitter.on(eventName, onEvent);
|
||||
return promise;
|
||||
|
||||
function onEvent(event) {
|
||||
--eventCount;
|
||||
if (eventCount)
|
||||
return;
|
||||
emitter.removeListener(eventName, onEvent);
|
||||
fulfill(event);
|
||||
}
|
||||
waitEvent: function(emitter, eventName) {
|
||||
return new Promise(fulfill => emitter.once(eventName, fulfill));
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user