aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2017-04-14 21:42:47 +0200
committerAnna Henningsen <anna@addaleax.net>2017-05-09 15:01:44 +0200
commite7c51454b0de0a6b33c15a4d77006a17ced4eeff (patch)
treed255139db748c4940224e6c672dbae6d372655ca /lib
parente965ed16c111f219116a5e8ea6847f5b98b5d0be (diff)
downloadandroid-node-v8-e7c51454b0de0a6b33c15a4d77006a17ced4eeff.tar.gz
android-node-v8-e7c51454b0de0a6b33c15a4d77006a17ced4eeff.tar.bz2
android-node-v8-e7c51454b0de0a6b33c15a4d77006a17ced4eeff.zip
timers: add promisify support
Add support for `util.promisify(setTimeout)` and `util.promisify(setImmediate)` as a proof-of-concept implementation. `clearTimeout()` and `clearImmediate()` do not work on those Promises. Includes documentation and tests. PR-URL: https://github.com/nodejs/node/pull/12442 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: William Kapke <william.kapke@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/timers.js26
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/timers.js b/lib/timers.js
index 115c3c8296..5d21227b7b 100644
--- a/lib/timers.js
+++ b/lib/timers.js
@@ -23,6 +23,8 @@
const TimerWrap = process.binding('timer_wrap').Timer;
const L = require('internal/linkedlist');
+const internalUtil = require('internal/util');
+const { createPromise, promiseResolve } = process.binding('util');
const assert = require('assert');
const util = require('util');
const debug = util.debuglog('timer');
@@ -364,7 +366,7 @@ exports.enroll = function(item, msecs) {
*/
-exports.setTimeout = function(callback, after, arg1, arg2, arg3) {
+function setTimeout(callback, after, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function');
}
@@ -383,8 +385,16 @@ exports.setTimeout = function(callback, after, arg1, arg2, arg3) {
}
return createSingleTimeout(callback, after, args);
+}
+
+setTimeout[internalUtil.promisify.custom] = function(after, value) {
+ const promise = createPromise();
+ createSingleTimeout(promise, after, [value]);
+ return promise;
};
+exports.setTimeout = setTimeout;
+
function createSingleTimeout(callback, after, args) {
after *= 1; // coalesce to number or NaN
if (!(after >= 1 && after <= TIMEOUT_MAX))
@@ -403,6 +413,8 @@ function createSingleTimeout(callback, after, args) {
function ontimeout(timer) {
var args = timer._timerArgs;
var callback = timer._onTimeout;
+ if (typeof callback !== 'function')
+ return promiseResolve(callback, args[0]);
if (!args)
callback.call(timer);
else {
@@ -687,6 +699,8 @@ function tryOnImmediate(immediate, oldTail) {
function runCallback(timer) {
const argv = timer._argv;
const argc = argv ? argv.length : 0;
+ if (typeof timer._callback !== 'function')
+ return promiseResolve(timer._callback, argv[0]);
switch (argc) {
// fast-path callbacks with 0-3 arguments
case 0:
@@ -715,7 +729,7 @@ function Immediate() {
this.domain = process.domain;
}
-exports.setImmediate = function(callback, arg1, arg2, arg3) {
+function setImmediate(callback, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function');
}
@@ -740,8 +754,16 @@ exports.setImmediate = function(callback, arg1, arg2, arg3) {
break;
}
return createImmediate(args, callback);
+}
+
+setImmediate[internalUtil.promisify.custom] = function(value) {
+ const promise = createPromise();
+ createImmediate([value], promise);
+ return promise;
};
+exports.setImmediate = setImmediate;
+
function createImmediate(args, callback) {
// declaring it `const immediate` causes v6.0.0 to deoptimize this function
var immediate = new Immediate();