From b456b7994e6b2c8294e6bfee032969cdbeb54d0a Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 2 Jul 2017 03:41:51 +0200 Subject: [PATCH] Simplify the code by using `p-finally` Closes #1 --- index.js | 11 ++++------- package.json | 3 +++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 41bc4c6..9748b9b 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ 'use strict'; +const pFinally = require('p-finally'); class TimeoutError extends Error { constructor(message) { @@ -24,14 +25,10 @@ module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => { reject(err); }, ms); - promise.then( - val => { + pFinally( + promise.then(resolve, reject), + () => { clearTimeout(timer); - resolve(val); - }, - err => { - clearTimeout(timer); - reject(err); } ); }); diff --git a/package.json b/package.json index 8c24e2e..840021f 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,9 @@ "cancel", "bluebird" ], + "dependencies": { + "p-finally": "^1.0.0" + }, "devDependencies": { "ava": "*", "delay": "^1.3.1",