From 4b8acf8cc26035594c5c0d4d84c38b3f3ca5d4a4 Mon Sep 17 00:00:00 2001 From: Alvaro Date: Tue, 27 Jun 2017 15:41:26 +0100 Subject: [PATCH] Refactoring --- index.js | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 41bc4c6..c994e3d 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ 'use strict'; +const delay = require('delay'); class TimeoutError extends Error { constructor(message) { @@ -12,7 +13,7 @@ module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => { throw new TypeError('Expected `ms` to be a positive number'); } - const timer = setTimeout(() => { + delay(ms).then(() => { if (typeof fallback === 'function') { resolve(fallback()); return; @@ -22,18 +23,7 @@ module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => { const err = fallback instanceof Error ? fallback : new TimeoutError(message); reject(err); - }, ms); - - promise.then( - val => { - clearTimeout(timer); - resolve(val); - }, - err => { - clearTimeout(timer); - reject(err); - } - ); + }); + promise.then(resolve, reject); }); - module.exports.TimeoutError = TimeoutError;