From fe922f05e4f7f6e1bfbb425b8c9c3d54c0462fb7 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Sat, 23 Dec 2023 11:23:43 +0100 Subject: [PATCH] timers: export timers.promises PR-URL: https://github.com/nodejs/node/pull/51246 Fixes: https://github.com/nodejs/node/issues/51237 Reviewed-By: Raz Luvaton Reviewed-By: James M Snell Reviewed-By: Anatoli Papirovski Reviewed-By: Chemi Atlow --- lib/timers.js | 16 +++++++++++++++- test/parallel/test-timers-promises.js | 12 ++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-timers-promises.js diff --git a/lib/timers.js b/lib/timers.js index ad58cf61f1309f..7c77ca5c04e2e0 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -24,6 +24,7 @@ const { MathTrunc, ObjectDefineProperty, + ObjectDefineProperties, SymbolDispose, SymbolToPrimitive, } = primordials; @@ -62,6 +63,7 @@ let debug = require('internal/util/debuglog').debuglog('timer', (fn) => { const { validateFunction } = require('internal/validators'); let timersPromises; +let timers; const { destroyHooksExist, @@ -347,7 +349,7 @@ Immediate.prototype[SymbolDispose] = function() { clearImmediate(this); }; -module.exports = { +module.exports = timers = { setTimeout, clearTimeout, setImmediate, @@ -372,3 +374,15 @@ module.exports = { 'timers.enroll() is deprecated. Please use setTimeout instead.', 'DEP0095'), }; + +ObjectDefineProperties(timers, { + promises: { + __proto__: null, + configurable: true, + enumerable: true, + get() { + timersPromises ??= require('timers/promises'); + return timersPromises; + }, + }, +}); diff --git a/test/parallel/test-timers-promises.js b/test/parallel/test-timers-promises.js new file mode 100644 index 00000000000000..6729bf12caf30e --- /dev/null +++ b/test/parallel/test-timers-promises.js @@ -0,0 +1,12 @@ +'use strict'; + +const common = require('../common'); + +const timer = require('node:timers'); +const timerPromises = require('node:timers/promises'); +const assert = require('assert'); +const { test } = require('node:test'); + +test('(node:timers/promises) is equal to (node:timers).promises', common.mustCall(() => { + assert.deepStrictEqual(timerPromises, timer.promises); +}));