Skip to content

Commit

Permalink
lib/index.js: awaitEmit - first rought implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
macbre committed Feb 1, 2019
1 parent c1f8a27 commit e5ea316
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e5ea316

Please sign in to comment.