From 65533961e2b05f480d08414d5a2aa8d85fdeed57 Mon Sep 17 00:00:00 2001 From: "trop[bot]" Date: Mon, 30 Jul 2018 13:02:59 +1000 Subject: [PATCH] fix: util.promisify(setTimeout) (#13858) --- lib/common/init.js | 19 +++++++++++++++---- spec/node-spec.js | 4 ++++ spec/static/main.js | 2 ++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/common/init.js b/lib/common/init.js index d732352a30514..b424768414b85 100644 --- a/lib/common/init.js +++ b/lib/common/init.js @@ -1,4 +1,5 @@ const timers = require('timers') +const util = require('util') process.atomBinding = require('./atom-binding-setup')(process.binding, process.type) @@ -8,11 +9,21 @@ process.atomBinding = require('./atom-binding-setup')(process.binding, process.t // which would delay the callbacks for arbitrary long time. So we should // initiatively activate the uv loop once setImmediate and process.nextTick is // called. -var wrapWithActivateUvLoop = function (func) { - return function () { - process.activateUvLoop() - return func.apply(this, arguments) +const wrapWithActivateUvLoop = function (func) { + return wrap(func, function (func) { + return function () { + process.activateUvLoop() + return func.apply(this, arguments) + } + }) +} + +function wrap (func, wrapper) { + const wrapped = wrapper(func) + if (func[util.promisify.custom]) { + wrapped[util.promisify.custom] = wrapper(func[util.promisify.custom]) } + return wrapped } process.nextTick = wrapWithActivateUvLoop(process.nextTick) diff --git a/spec/node-spec.js b/spec/node-spec.js index 3c6b19e4962b9..572c9548c1d2a 100644 --- a/spec/node-spec.js +++ b/spec/node-spec.js @@ -203,6 +203,10 @@ describe('node feature', () => { it('can be scheduled in time', (done) => { remote.getGlobal('setTimeout')(done, 0) }) + + it('can be promisified', (done) => { + remote.getGlobal('setTimeoutPromisified')(0).then(done) + }) }) describe('setInterval called under Chromium event loop in browser process', () => { diff --git a/spec/static/main.js b/spec/static/main.js index 4a4dfd109df33..f91dc4e521677 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -71,6 +71,8 @@ ipcMain.on('echo', function (event, msg) { event.returnValue = msg }) +global.setTimeoutPromisified = util.promisify(setTimeout) + const coverage = new Coverage({ outputPath: path.join(__dirname, '..', '..', 'out', 'coverage') })