diff --git a/index.js b/index.js index 9748b9b..50ca1da 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ -'use strict'; -const pFinally = require('p-finally'); +"use strict"; +const pFinally = require("p-finally"); class TimeoutError extends Error { constructor(message) { @@ -22,6 +22,10 @@ module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => { const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${ms} milliseconds`; const err = fallback instanceof Error ? fallback : new TimeoutError(message); + if (typeof promise.cancel === 'function') { + promise.cancel(err); + } + reject(err); }, ms);