ts-retry icon indicating copy to clipboard operation
ts-retry copied to clipboard

[Feature request] Get the last result when attempts are exhausted.

Open Azq2 opened this issue 1 year ago • 0 comments

Something like this:

export async function retryAsync<T>(callback: () => Promise<T>, options: { max: number, until: (lastResult: T) => boolean }) {
	let lastResult: T;
	for (let i = 0; i < options.max; i++) {
		lastResult = await callback();
		if (!options.until(lastResult))
			break;
	}
	return lastResult!;
}

Azq2 avatar Mar 02 '25 20:03 Azq2