From 39c5c200ba18ae80961d73bac0c27c06f523b547 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 30 Jul 2018 03:14:04 +0200 Subject: [PATCH] fix: util.promisify(setTimeout) (#13840) --- 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 d732352a3051..b424768414b8 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 b87ed189633a..9bb03494a56f 100644 --- a/spec/node-spec.js +++ b/spec/node-spec.js @@ -179,6 +179,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 d920426df3d9..2715e3f9a207 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -79,6 +79,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') })