Skip to content

process() does not return handler’s result #13

@3aluw

Description

@3aluw

Description

When using PagePool.process(), the return value from the handler is always lost.
This makes it impossible to directly get data back from the handler, which is a common need when scraping or processing pages.

Example

const pagePool = new PagePool();
await pagePool.launch();

const title = await pagePool.process(async (page) => {
  await page.goto("https://example.com");
  return await page.title();
});

console.log(title); // ❌ undefined (expected the page title)

Source of the Issue

Looking at the implementation:

async process(handler, ...args) {
  if (!this.pool) {
    Helpers.debug('pool is not found. Did you forgot to call launch() method?');
  } else if (typeof handler !== 'function') {
    Helpers.debug('handler is not a function');
  } else {
    await this.pool.use((page) => handler(page, ...args, this.pool))
      .catch((ex) => Helpers.debug('process error: %s', ex));
  }
}
  • The handler’s return value is not returned by process().
  • Instead, it’s awaited and discarded.

Suggested Solution

Modify the process method to return the handler’s result:

async process(handler, ...args) {
  if (!this.pool) {
    Helpers.debug('pool is not found. Did you forgot to call launch() method?');
  } else if (typeof handler !== 'function') {
    Helpers.debug('handler is not a function');
  } else {
    return this.pool.use((page) => handler(page, ...args, this.pool));
  }
}

With this change, the earlier example would correctly log the title.

Workaround

Until this is fixed, users can wrap process() manually:

async function processWithResult(pool, handler, ...args) {
  let result;
  await pool.process(async (page, ...innerArgs) => {
    result = await handler(page, ...innerArgs);
  }, ...args);
  return result;
}

// Usage
const title = await processWithResult(pagePool, async (page) => {
  await page.goto("https://example.com");
  return await page.title();
});

console.log(title); // ✅ works

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions