diff --git a/lib/index.js b/lib/index.js index e5b60bc14..5888be6a9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -157,7 +157,23 @@ function phantomas(url, opts) { loader.loadModules(scope); // browser's scope and modules are set up, you can now use it in your modules - events.emit('init', browser, page); + //events.emit('init', browser, page); + + // TODO: introduce awaitEmit + var eventPromises = []; + + events.listeners('init').forEach(async fn => { + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction + const ret = fn(browser, page); + + if (ret instanceof Promise) { + eventPromises.push(ret); + } + + // console.log(fn, fn.toString(), ret, ret instanceof Promise); + }); + + await Promise.all(eventPromises); // https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pagegotourl-options const waitUntil = options['wait-for-network-idle'] ? 'networkidle0' : undefined,