-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
317 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* Retry a given promise with gradually increasing delay. | ||
* | ||
* @param promise a function that returns a promise to retry | ||
* @param maxRetries the maximum number of times to retry | ||
* @param initialDelay the initial value to delay before retrying (in milliseconds) | ||
* @returns a promise resolving to the result of the promise from the promise generating function, or undefined | ||
*/ | ||
export async function retry<T>( | ||
promise: (n: number) => Promise<T>, | ||
maxRetries: number, | ||
initialDelay = 500 | ||
): Promise<T | undefined> { | ||
let delay = initialDelay, | ||
n = 1, | ||
maybeError: Error | undefined; | ||
|
||
// Take care of negative or zero | ||
maxRetries = Math.max(maxRetries, 1); | ||
|
||
while (n <= maxRetries) { | ||
try { | ||
// Note: return await intentional so we can catch errors | ||
return await promise(n); | ||
} catch (err) { | ||
maybeError = err; | ||
|
||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
|
||
delay *= n; | ||
n++; | ||
} | ||
} | ||
|
||
if (maybeError) { | ||
throw maybeError; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
const assert = require('assert'); | ||
const sinon = require('sinon'); | ||
const { retry } = require('../'); | ||
|
||
describe('retry', function () { | ||
it('succeeds on first try', async function () { | ||
const fake = sinon.fake((n) => Promise.resolve(n)); | ||
assert.strictEqual(await retry(fake, 3, 0), 1); | ||
assert.strictEqual(fake.callCount, 1); | ||
}); | ||
|
||
it('handles zero retries', async function () { | ||
const fake = sinon.fake((n) => Promise.resolve(n)); | ||
assert.strictEqual(await retry(fake, 0, 0), 1); | ||
assert.strictEqual(fake.callCount, 1); | ||
}); | ||
|
||
it('handles negative retries', async function () { | ||
const fake = sinon.fake((n) => Promise.resolve(n)); | ||
assert.strictEqual(await retry(fake, -10, 0), 1); | ||
assert.strictEqual(fake.callCount, 1); | ||
}); | ||
|
||
it('succeeds eventually', async function () { | ||
const fake = sinon.fake((n) => (n < 3 ? Promise.reject() : Promise.resolve(10))); | ||
assert.strictEqual(await retry(fake, 3, 0), 10); | ||
assert.strictEqual(fake.callCount, 3); | ||
}); | ||
|
||
it('yields error if never succeeds', async function () { | ||
const fake = sinon.fake(() => Promise.reject(new Error('oh no'))); | ||
await assert.rejects(retry(fake, 3, 0), { | ||
name: 'Error', | ||
message: 'oh no', | ||
}); | ||
assert.strictEqual(fake.callCount, 3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.