Skip to content

Commit

Permalink
fix: util.promisify(setTimeout) (#13858)
Browse files Browse the repository at this point in the history
  • Loading branch information
trop[bot] authored and MarshallOfSound committed Jul 30, 2018
1 parent 77093d5 commit 6553396
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
19 changes: 15 additions & 4 deletions 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)

Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions spec/node-spec.js
Expand Up @@ -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', () => {
Expand Down
2 changes: 2 additions & 0 deletions spec/static/main.js
Expand Up @@ -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')
})
Expand Down

0 comments on commit 6553396

Please sign in to comment.