From dbffc3c35c390d7f9f3af2f4ea962625a01a0252 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Wed, 14 Jun 2017 08:21:56 -0700 Subject: [PATCH] Await promises returned from the inPageCallback This patch teaches puppeteer to await promises returned from the inPageCallback. --- lib/Page.js | 2 +- test/test.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/Page.js b/lib/Page.js index 9511e191..e85673ca 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -144,7 +144,7 @@ class Page extends EventEmitter { var name = inPageCallback.name; var callback = inPageCallback.callback; var args = await this.evaluate(callbackName => window[callbackName].__args, name); - var result = callback.apply(null, args); + var result = await Promise.resolve(callback.apply(null, args)); await this.evaluate(assignResult, name, result); this._client.send('Debugger.resume'); diff --git a/test/test.js b/test/test.js index 283906fa..a5330655 100644 --- a/test/test.js +++ b/test/test.js @@ -73,6 +73,16 @@ describe('Puppeteer', function() { }); expect(result).toBe(36); })); + it('should await returned promise', SX(async function() { + await page.setInPageCallback('callController', function(a, b) { + return Promise.resolve(a * b); + }); + + var result = await page.evaluate(function() { + return callController(3, 5); + }); + expect(result).toBe(15); + })); }); });