From b44967b72c7a13bd6488eb296a250a4990e89b12 Mon Sep 17 00:00:00 2001 From: marco-ippolito Date: Thu, 21 Dec 2023 11:14:56 +0100 Subject: [PATCH] timers: export timers.promises --- 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); +}));