forked from binded/phantom-pool
-
Couldn't load subscription status.
- Fork 38
Open
Description
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); // ✅ worksMetadata
Metadata
Assignees
Labels
No labels