diff --git a/lib/FrameManager.js b/lib/FrameManager.js index 7a987aee217..adf5f556760 100644 --- a/lib/FrameManager.js +++ b/lib/FrameManager.js @@ -178,25 +178,28 @@ class FrameManager extends EventEmitter { * @return {!Promise} */ async _waitForInFrame(selector, frame) { - let code = selector => { - let promise = new Promise(fulfill => { + + function code(selector) { + if (document.querySelector(selector)) + return Promise.resolve(); + + let callback; + const result = new Promise(fulfill => callback = fulfill); + + const mo = new MutationObserver((mutations, observer) => { if (document.querySelector(selector)) { - fulfill(); + observer.disconnect(); + callback(); return; } - new MutationObserver((mutations, observer) => { - if (document.querySelector(selector)) { - observer.disconnect(); - fulfill(); - return; - } - }).observe(document.documentElement, { - childList: true, - subtree: true - }); }); - return promise; - }; + mo.observe(document.documentElement, { + childList: true, + subtree: true + }); + return result; + } + let contextId = undefined; if (!frame.isMainFrame()) { contextId = this._frameIdToExecutionContextId.get(frame._id);