promise-chain-executor
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -1,3 +0,4 @@ | ||
export declare const index: { | ||
execute: <T>(promiseSupplier: (previousResult: T | null) => Promise<T | null>) => Promise<Array<T>>; | ||
export type PromiseSupplier<T> = (previousResult: T | null) => Promise<T | null>; | ||
export declare const promiseChainExecutor: { | ||
execute: <T>(promiseSupplier: PromiseSupplier<T>) => Promise<Array<T>>; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.index = void 0; | ||
const execute = (promiseSupplier) => { | ||
exports.promiseChainExecutor = void 0; | ||
const execute = async (promiseSupplier) => { | ||
const results = []; | ||
@@ -10,3 +10,3 @@ const handleError = (err) => { | ||
}; | ||
const handleResult = (result) => { | ||
const handleResult = async (result) => { | ||
if (result === null) { | ||
@@ -16,6 +16,11 @@ return Promise.resolve(results); | ||
results.push(result); | ||
const nextPromise = promiseSupplier(result); | ||
return nextPromise | ||
.then(handleResult) | ||
.catch(handleError); | ||
try { | ||
const nextPromise = promiseSupplier(result); | ||
const nextResult = await nextPromise; | ||
return await handleResult(nextResult); | ||
} | ||
catch (err) { | ||
handleError(err); | ||
throw err; | ||
} | ||
}; | ||
@@ -26,8 +31,13 @@ const initialPromise = promiseSupplier(null); | ||
} | ||
return initialPromise | ||
.then(handleResult) | ||
.catch(handleError); | ||
try { | ||
const initialResult = await initialPromise; | ||
return await handleResult(initialResult); | ||
} | ||
catch (err) { | ||
handleError(err); | ||
throw err; | ||
} | ||
}; | ||
exports.index = { | ||
exports.promiseChainExecutor = { | ||
execute, | ||
}; |
@@ -14,3 +14,3 @@ "use strict"; | ||
}; | ||
const results = await index_1.index.execute(promiseSupplier); | ||
const results = await index_1.promiseChainExecutor.execute(promiseSupplier); | ||
expect(results).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
@@ -17,0 +17,0 @@ }); |
{ | ||
"name": "promise-chain-executor", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Executes promises in a chain, allowing results from previous promise to affect following ones", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -24,2 +24,3 @@ | ||
handleError(err) | ||
throw err | ||
} | ||
@@ -38,2 +39,3 @@ }; | ||
handleError(err) | ||
throw err | ||
} | ||
@@ -40,0 +42,0 @@ }; |
7249
129