Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+36
@@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
const pFinally = require('p-finally');
|
||||
|
||||
class TimeoutError extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.name = 'TimeoutError';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => {
|
||||
if (typeof ms !== 'number' || ms < 0) {
|
||||
throw new TypeError('Expected `ms` to be a positive number');
|
||||
}
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
if (typeof fallback === 'function') {
|
||||
resolve(fallback());
|
||||
return;
|
||||
}
|
||||
|
||||
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${ms} milliseconds`;
|
||||
const err = fallback instanceof Error ? fallback : new TimeoutError(message);
|
||||
|
||||
reject(err);
|
||||
}, ms);
|
||||
|
||||
pFinally(
|
||||
promise.then(resolve, reject),
|
||||
() => {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
module.exports.TimeoutError = TimeoutError;
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "p-timeout",
|
||||
"version": "1.2.1",
|
||||
"description": "Timeout a promise after a specified amount of time",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/p-timeout",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"keywords": [
|
||||
"promise",
|
||||
"timeout",
|
||||
"error",
|
||||
"invalidate",
|
||||
"async",
|
||||
"await",
|
||||
"promises",
|
||||
"time",
|
||||
"out",
|
||||
"cancel",
|
||||
"bluebird"
|
||||
],
|
||||
"dependencies": {
|
||||
"p-finally": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "*",
|
||||
"delay": "^2.0.0",
|
||||
"xo": "*"
|
||||
}
|
||||
|
||||
,"_resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-1.2.1.tgz"
|
||||
,"_integrity": "sha1-XrOzU7f86Z8QGhA4iAuwVOu+o4Y="
|
||||
,"_from": "p-timeout@^1.1.1"
|
||||
}
|
||||
Reference in new issue
Block a user