Skip to content

Commit 6be66a6

Browse files
committed
fix(javascript): allow async param on createIterablePromise
1 parent 8d9696f commit 6be66a6

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

clients/algoliasearch-client-javascript/packages/client-common/src/createIterablePromise.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@ export function createIterablePromise<TResponse>({
2020
const retry = (previousResponse?: TResponse): Promise<TResponse> => {
2121
return new Promise<TResponse>((resolve, reject) => {
2222
func(previousResponse)
23-
.then((response) => {
23+
.then(async (response) => {
2424
if (aggregator) {
25-
aggregator(response);
25+
await aggregator(response);
2626
}
2727

28-
if (validate(response)) {
28+
if (await validate(response)) {
2929
return resolve(response);
3030
}
3131

32-
if (error && error.validate(response)) {
33-
return reject(new Error(error.message(response)));
32+
if (error && (await error.validate(response))) {
33+
return reject(new Error(await error.message(response)));
3434
}
3535

36-
return setTimeout(() => {
37-
retry(response).then(resolve).catch(reject);
38-
}, timeout());
36+
return setTimeout(
37+
() => {
38+
retry(response).then(resolve).catch(reject);
39+
},
40+
await timeout(),
41+
);
3942
})
4043
.catch((err) => {
4144
reject(err);

clients/algoliasearch-client-javascript/packages/client-common/src/types/createIterablePromise.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export type IterableOptions<TResponse> = Partial<{
22
/**
33
* The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
44
*/
5-
aggregator: (response: TResponse) => void;
5+
aggregator: (response: TResponse) => unknown | PromiseLike<unknown>;
66

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

1616
/**
1717
* The error message to throw.
1818
*/
19-
message: (response: TResponse) => string;
19+
message: (response: TResponse) => string | PromiseLike<string>;
2020
};
2121

2222
/**
2323
* The function to decide how long to wait between iterations.
2424
*/
25-
timeout: () => number;
25+
timeout: () => number | PromiseLike<number>;
2626
}>;
2727

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

0 commit comments

Comments
 (0)