Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ export function createIterablePromise<TResponse>({
const retry = (previousResponse?: TResponse): Promise<TResponse> => {
return new Promise<TResponse>((resolve, reject) => {
func(previousResponse)
.then((response) => {
.then(async (response) => {
if (aggregator) {
aggregator(response);
await aggregator(response);
}

if (validate(response)) {
if (await validate(response)) {
return resolve(response);
}

if (error && error.validate(response)) {
return reject(new Error(error.message(response)));
if (error && (await error.validate(response))) {
return reject(new Error(await error.message(response)));
}

return setTimeout(() => {
retry(response).then(resolve).catch(reject);
}, timeout());
return setTimeout(
() => {
retry(response).then(resolve).catch(reject);
},
await timeout(),
);
})
.catch((err) => {
reject(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type IterableOptions<TResponse> = Partial<{
/**
* The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
*/
aggregator: (response: TResponse) => void;
aggregator: (response: TResponse) => unknown | PromiseLike<unknown>;

/**
* The `validate` condition to throw an error and its message.
Expand All @@ -11,18 +11,18 @@ export type IterableOptions<TResponse> = Partial<{
/**
* The function to validate the error condition.
*/
validate: (response: TResponse) => boolean;
validate: (response: TResponse) => boolean | PromiseLike<boolean>;

/**
* The error message to throw.
*/
message: (response: TResponse) => string;
message: (response: TResponse) => string | PromiseLike<string>;
};

/**
* The function to decide how long to wait between iterations.
*/
timeout: () => number;
timeout: () => number | PromiseLike<number>;
}>;

export type CreateIterablePromise<TResponse> = IterableOptions<TResponse> & {
Expand All @@ -36,5 +36,5 @@ export type CreateIterablePromise<TResponse> = IterableOptions<TResponse> & {
/**
* The validator function. It receive the resolved return of the API call.
*/
validate: (response: TResponse) => boolean;
validate: (response: TResponse) => boolean | PromiseLike<boolean>;
};
Loading