From e5ea3165e0be251278b8bb24476320a26949e095 Mon Sep 17 00:00:00 2001 From: macbre Date: Fri, 1 Feb 2019 22:28:26 +0100 Subject: [PATCH] lib/index.js: awaitEmit - first rought implementation --- lib/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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,