From 1b74a08eba47236c0efc494fbd7a4227b3fec600 Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Tue, 22 Dec 2020 23:23:23 +0800 Subject: [PATCH] timers: refactor to use validateAbortSignal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/36604 Backport-PR-URL: https://github.com/nodejs/node/pull/38386 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rich Trott Reviewed-By: Juan José Arboleda Reviewed-By: Richard Lau --- lib/internal/timers/promises.js | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/internal/timers/promises.js b/lib/internal/timers/promises.js index ef1e6437d4f6ca..94c8f40a19db5e 100644 --- a/lib/internal/timers/promises.js +++ b/lib/internal/timers/promises.js @@ -16,6 +16,8 @@ const { codes: { ERR_INVALID_ARG_TYPE } } = require('internal/errors'); +const { validateAbortSignal } = require('internal/validators'); + let DOMException; const lazyDOMException = hideStackFrames((message, name) => { @@ -34,15 +36,10 @@ function setTimeout(after, value, options = {}) { options)); } const { signal, ref = true } = options; - if (signal !== undefined && - (signal === null || - typeof signal !== 'object' || - !('aborted' in signal))) { - return PromiseReject( - new ERR_INVALID_ARG_TYPE( - 'options.signal', - 'AbortSignal', - signal)); + try { + validateAbortSignal(signal, 'options.signal'); + } catch (err) { + return PromiseReject(err); } if (typeof ref !== 'boolean') { return PromiseReject( @@ -83,15 +80,10 @@ function setImmediate(value, options = {}) { options)); } const { signal, ref = true } = options; - if (signal !== undefined && - (signal === null || - typeof signal !== 'object' || - !('aborted' in signal))) { - return PromiseReject( - new ERR_INVALID_ARG_TYPE( - 'options.signal', - 'AbortSignal', - signal)); + try { + validateAbortSignal(signal, 'options.signal'); + } catch (err) { + return PromiseReject(err); } if (typeof ref !== 'boolean') { return PromiseReject(