Browse Source

fix: util.promisify(setTimeout) (#13859)

trop[bot] 6 years ago
parent
commit
237ad9a49d
3 changed files with 21 additions and 4 deletions
  1. 15 4
      lib/common/init.js
  2. 4 0
      spec/node-spec.js
  3. 2 0
      spec/static/main.js

+ 15 - 4
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)

+ 4 - 0
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', () => {

+ 2 - 0
spec/static/main.js

@@ -76,6 +76,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')
 })