Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-assist

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-assist - npm Package Compare versions

Comparing version 1.2.3 to 1.2.4

2

cjs/retry.js

@@ -63,5 +63,5 @@ "use strict";

function waitFor(action, options) {
return retry(action, Object.assign({ delay: 10, timeout: 500, retries: Infinity }, options));
return retry(action, Object.assign({ delay: 10, timeout: 1000, retries: Infinity }, options));
}
exports.waitFor = waitFor;
//# sourceMappingURL=retry.js.map

@@ -9,10 +9,10 @@ /**

export function deferred() {
var resolve;
var reject;
var promise = new Promise(function (res, rej) {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { promise: promise, resolve: resolve, reject: reject };
return { promise, resolve, reject };
}
//# sourceMappingURL=deferred.js.map

@@ -1,49 +0,3 @@

var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { sleep } from './sleep';
var defaultOptions = {
const defaultOptions = {
retries: 3,

@@ -61,69 +15,49 @@ delay: 0,

*/
export function retry(action, options) {
return __awaiter(this, void 0, void 0, function () {
var _a, retries, delay, timeout, lastError, timedOut, timeoutId, timeoutPromise, maxAttempts, attemptCount, actionResult, result, e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = __assign({}, defaultOptions, options), retries = _a.retries, delay = _a.delay, timeout = _a.timeout;
timedOut = false;
timeoutPromise = new Promise(function (_res, rej) {
if (timeout > 0) {
timeoutId = setTimeout(function () {
timedOut = true;
if (!lastError) {
lastError = new Error("timed out after " + timeout + "ms");
}
rej();
}, timeout);
}
});
maxAttempts = retries + 1;
attemptCount = 0;
_c.label = 1;
case 1:
attemptCount++;
_c.label = 2;
case 2:
_c.trys.push([2, 4, , 5]);
actionResult = action();
if (actionResult instanceof Promise) {
// make sure we always save error of original promise
// Promise.race below might loose it due to timeout
actionResult.catch(function (e) { return lastError = e || lastError; });
}
return [4 /*yield*/, Promise.race([actionResult, timeoutPromise])];
case 3:
result = _c.sent();
clearTimeout(timeoutId);
return [2 /*return*/, result];
case 4:
e_1 = _c.sent();
lastError = e_1 || lastError;
return [3 /*break*/, 5];
case 5:
if (!(delay > 0)) return [3 /*break*/, 9];
_c.label = 6;
case 6:
_c.trys.push([6, 8, , 9]);
return [4 /*yield*/, Promise.race([sleep(delay), timeoutPromise])];
case 7:
_c.sent();
return [3 /*break*/, 9];
case 8:
_b = _c.sent();
return [3 /*break*/, 9];
case 9:
if (!timedOut && (attemptCount < maxAttempts)) return [3 /*break*/, 1];
_c.label = 10;
case 10:
clearTimeout(timeoutId);
throw (lastError || new Error("failed after " + attemptCount + " tries"));
export async function retry(action, options) {
const { retries, delay, timeout } = Object.assign({}, defaultOptions, options);
let lastError; // we expose last error if all attempts failed
let timedOut = false;
let timeoutId; // so we can cancel the timeout rejection
const timeoutPromise = new Promise((_res, rej) => {
if (timeout > 0) {
timeoutId = setTimeout(() => {
timedOut = true;
if (!lastError) {
lastError = new Error(`timed out after ${timeout}ms`);
}
rej();
}, timeout);
}
});
const maxAttempts = retries + 1;
let attemptCount = 0;
do {
attemptCount++;
try {
const actionResult = action();
if (actionResult instanceof Promise) {
// make sure we always save error of original promise
// Promise.race below might loose it due to timeout
actionResult.catch(e => lastError = e || lastError);
}
});
});
const result = await Promise.race([actionResult, timeoutPromise]);
clearTimeout(timeoutId);
return result;
}
catch (e) {
lastError = e || lastError;
}
if (delay > 0) {
try {
await Promise.race([sleep(delay), timeoutPromise]);
}
catch ( /* we throw lastError at the end */_a) { /* we throw lastError at the end */ }
}
} while (!timedOut && (attemptCount < maxAttempts));
clearTimeout(timeoutId);
throw (lastError || new Error(`failed after ${attemptCount} tries`));
}
export function waitFor(action, options) {
return retry(action, __assign({ delay: 10, timeout: 500, retries: Infinity }, options));
return retry(action, Object.assign({ delay: 10, timeout: 1000, retries: Infinity }, options));
}
//# sourceMappingURL=retry.js.map

@@ -8,4 +8,4 @@ /**

export function sleep(ms) {
return new Promise(function (resolve) { return setTimeout(resolve, ms); });
return new Promise(resolve => setTimeout(resolve, ms));
}
//# sourceMappingURL=sleep.js.map

@@ -11,10 +11,9 @@ /**

*/
export function timeout(originalPromise, ms, timeoutMessage) {
if (timeoutMessage === void 0) { timeoutMessage = "timed out after " + ms + "ms"; }
return new Promise(function (resolve, reject) {
var timerId = setTimeout(function () { return reject(new Error(timeoutMessage)); }, ms);
originalPromise.then(function (resolvedValue) {
export function timeout(originalPromise, ms, timeoutMessage = `timed out after ${ms}ms`) {
return new Promise((resolve, reject) => {
const timerId = setTimeout(() => reject(new Error(timeoutMessage)), ms);
originalPromise.then(resolvedValue => {
clearTimeout(timerId);
resolve(resolvedValue);
}, function (rejectReason) {
}, rejectReason => {
clearTimeout(timerId);

@@ -21,0 +20,0 @@ reject(rejectReason);

{
"name": "promise-assist",
"description": "Several helper functions when working with native promises",
"version": "1.2.3",
"version": "1.2.4",
"main": "./cjs/index.js",

@@ -10,8 +10,7 @@ "module": "./esm/index.js",

"clean": "rimraf ./cjs ./esm",
"build": "run-p build:cjs build:esm",
"prebuild": "yarn clean",
"build": "yarn build:cjs && yarn build:esm",
"build:cjs": "tsc -p src --outDir cjs --module commonjs",
"build:esm": "tsc -p src --outDir esm --module esnext --target es5",
"lint": "run-p lint:src lint:test",
"lint:src": "tslint -p src",
"lint:test": "tslint -p test",
"build:esm": "tsc -p src --outDir esm --module esnext",
"lint": "tslint -p tsconfig.base.json",
"pretest": "yarn lint",

@@ -28,7 +27,6 @@ "test": "mocha -r @ts-tools/node/r \"test/**/*.spec.ts\" --watch-extensions ts",

"chai-as-promised": "^7.1.1",
"mocha": "^6.0.0",
"npm-run-all": "^4.1.5",
"mocha": "^6.0.2",
"rimraf": "^2.6.3",
"tslint": "^5.12.1",
"typescript": "~3.3.3"
"tslint": "^5.14.0",
"typescript": "~3.4.1"
},

@@ -35,0 +33,0 @@ "files": [

@@ -1,8 +0,8 @@

export type PromiseResolveCb<T> = (value?: T | PromiseLike<T>) => void
export type PromiseRejectCb = (reason?: any) => void
export type PromiseResolveCb<T> = (value?: T | PromiseLike<T>) => void;
export type PromiseRejectCb = (reason?: any) => void;
export interface IDeferredPromise<T> {
promise: Promise<T>
resolve: PromiseResolveCb<T>
reject: PromiseRejectCb
promise: Promise<T>;
resolve: PromiseResolveCb<T>;
reject: PromiseRejectCb;
}

@@ -18,10 +18,10 @@

export function deferred<T = void>(): IDeferredPromise<T> {
let resolve!: PromiseResolveCb<T>
let reject!: PromiseRejectCb
let resolve!: PromiseResolveCb<T>;
let reject!: PromiseRejectCb;
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
resolve = res;
reject = rej;
});
return { promise, resolve, reject }
return { promise, resolve, reject };
}

@@ -1,4 +0,4 @@

export * from './deferred'
export * from './retry'
export * from './sleep'
export * from './timeout'
export * from './deferred';
export * from './retry';
export * from './sleep';
export * from './timeout';

@@ -1,2 +0,2 @@

import { sleep } from './sleep'
import { sleep } from './sleep';

@@ -10,3 +10,3 @@ export interface IRetryOptions {

*/
retries?: number
retries?: number;

@@ -18,3 +18,3 @@ /**

*/
delay?: number
delay?: number;

@@ -26,3 +26,3 @@ /**

*/
timeout?: number
timeout?: number;
}

@@ -34,3 +34,3 @@

timeout: 0
}
};

@@ -49,7 +49,7 @@ /**

): Promise<T> {
const { retries, delay, timeout } = { ...defaultOptions, ...options }
const { retries, delay, timeout } = { ...defaultOptions, ...options };
let lastError: Error | undefined // we expose last error if all attempts failed
let timedOut = false
let timeoutId: number | undefined // so we can cancel the timeout rejection
let lastError: Error | undefined; // we expose last error if all attempts failed
let timedOut = false;
let timeoutId: ReturnType<typeof setTimeout> | undefined; // so we can cancel the timeout rejection

@@ -59,36 +59,36 @@ const timeoutPromise = new Promise<T>((_res, rej) => {

timeoutId = setTimeout(() => {
timedOut = true
timedOut = true;
if (!lastError) {
lastError = new Error(`timed out after ${timeout}ms`)
lastError = new Error(`timed out after ${timeout}ms`);
}
rej()
}, timeout)
rej();
}, timeout);
}
})
});
const maxAttempts = retries + 1
let attemptCount = 0
const maxAttempts = retries + 1;
let attemptCount = 0;
do {
attemptCount++
attemptCount++;
try {
const actionResult = action()
const actionResult = action();
if (actionResult instanceof Promise) {
// make sure we always save error of original promise
// Promise.race below might loose it due to timeout
actionResult.catch(e => lastError = e || lastError)
actionResult.catch(e => lastError = e || lastError);
}
const result = await Promise.race([actionResult, timeoutPromise])
clearTimeout(timeoutId)
return result
} catch (e) { lastError = e || lastError }
const result = await Promise.race([actionResult, timeoutPromise]);
clearTimeout(timeoutId);
return result;
} catch (e) { lastError = e || lastError; }
if (delay > 0) {
try {
await Promise.race([sleep(delay), timeoutPromise])
await Promise.race([sleep(delay), timeoutPromise]);
} catch { /* we throw lastError at the end */ }
}
} while (!timedOut && (attemptCount < maxAttempts))
} while (!timedOut && (attemptCount < maxAttempts));
clearTimeout(timeoutId)
throw (lastError || new Error(`failed after ${attemptCount} tries`))
clearTimeout(timeoutId);
throw (lastError || new Error(`failed after ${attemptCount} tries`));
}

@@ -100,3 +100,3 @@

): Promise<T> {
return retry(action, { delay: 10, timeout: 500, retries: Infinity, ...options })
return retry(action, { delay: 10, timeout: 1000, retries: Infinity, ...options });
}

@@ -8,3 +8,3 @@ /**

export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
return new Promise(resolve => setTimeout(resolve, ms));
}

@@ -17,14 +17,15 @@ /**

return new Promise((resolve, reject) => {
const timerId = setTimeout(() => reject(new Error(timeoutMessage)), ms)
const timerId = setTimeout(() => reject(new Error(timeoutMessage)), ms);
originalPromise.then(
resolvedValue => {
clearTimeout(timerId)
resolve(resolvedValue)
clearTimeout(timerId);
resolve(resolvedValue);
},
rejectReason => {
clearTimeout(timerId)
reject(rejectReason)
clearTimeout(timerId);
reject(rejectReason);
}
)
})
);
});
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc