File tree Expand file tree Collapse file tree 1 file changed +9
-9
lines changed
Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change 11/// <reference path='../../../third_party/typings/browser.d.ts' />
22
33// Invokes f up to maxAttempts number of times, resolving with its result
4- // on the first success and rejecting on maxAttempts-th failure.
5- export const retry = < T > ( f : ( ) => Promise < T > , maxAttempts : number ) : Promise < T > => {
6- return f ( ) . catch ( ( e : Error ) => {
7- -- maxAttempts ;
8- if ( maxAttempts > 0 ) {
9- return retry ( f , maxAttempts ) ;
10- } else {
11- return Promise . reject ( e ) ;
12- }
4+ // on the first success and rejecting on the maxAttempts-th failure, waiting,
5+ // if specified, intervalMs ms between each attempt.
6+ export const retry = < T > ( f : ( ) => Promise < T > , maxAttempts : number , intervalMs = 0 ) : Promise < T > => {
7+ return f ( ) . catch ( ( e : Error ) => {
8+ return maxAttempts <= 1 ? Promise . reject ( e ) : new Promise < T > ( ( F , R ) => {
9+ setTimeout ( ( ) => {
10+ retry ( f , maxAttempts - 1 , intervalMs ) . then ( F , R ) ;
11+ } , intervalMs ) ;
12+ } ) ;
1313 } ) ;
1414} ;
1515
You can’t perform that action at this time.
0 commit comments