Skip to content

Commit 237ad9a

Browse files
trop[bot]MarshallOfSound
authored andcommitted
fix: util.promisify(setTimeout) (electron#13859)
1 parent 45158bd commit 237ad9a

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

lib/common/init.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const timers = require('timers')
2+
const util = require('util')
23

34
process.atomBinding = require('./atom-binding-setup')(process.binding, process.type)
45

@@ -8,11 +9,21 @@ process.atomBinding = require('./atom-binding-setup')(process.binding, process.t
89
// which would delay the callbacks for arbitrary long time. So we should
910
// initiatively activate the uv loop once setImmediate and process.nextTick is
1011
// called.
11-
var wrapWithActivateUvLoop = function (func) {
12-
return function () {
13-
process.activateUvLoop()
14-
return func.apply(this, arguments)
12+
const wrapWithActivateUvLoop = function (func) {
13+
return wrap(func, function (func) {
14+
return function () {
15+
process.activateUvLoop()
16+
return func.apply(this, arguments)
17+
}
18+
})
19+
}
20+
21+
function wrap (func, wrapper) {
22+
const wrapped = wrapper(func)
23+
if (func[util.promisify.custom]) {
24+
wrapped[util.promisify.custom] = wrapper(func[util.promisify.custom])
1525
}
26+
return wrapped
1627
}
1728

1829
process.nextTick = wrapWithActivateUvLoop(process.nextTick)

spec/node-spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ describe('node feature', () => {
179179
it('can be scheduled in time', (done) => {
180180
remote.getGlobal('setTimeout')(done, 0)
181181
})
182+
183+
it('can be promisified', (done) => {
184+
remote.getGlobal('setTimeoutPromisified')(0).then(done)
185+
})
182186
})
183187

184188
describe('setInterval called under Chromium event loop in browser process', () => {

spec/static/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ ipcMain.on('echo', function (event, msg) {
7676
event.returnValue = msg
7777
})
7878

79+
global.setTimeoutPromisified = util.promisify(setTimeout)
80+
7981
const coverage = new Coverage({
8082
outputPath: path.join(__dirname, '..', '..', 'out', 'coverage')
8183
})

0 commit comments

Comments
 (0)