This commit is contained in:
Mikhail Silaev 2017-11-07 18:19:52 +03:00
parent 339b0ac5db
commit 3a7b0ebc43
1 changed files with 6 additions and 2 deletions

View File

@ -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);