diff --git a/README.md b/README.md index c7bba32..de9d28a 100644 --- a/README.md +++ b/README.md @@ -122,12 +122,20 @@ await castDecoder.getSpell( ```js import { retry } from '@instadapp/utils' -retry(() => asyncCall(), { + retry(() => asyncCall(), { timeouts: [5_000, 10_000, 15_000], // timeouts for each retry attempt in ms delay: 300 // delay between retries in ms }) ``` +### wait + +```js +import { wait } from '@instadapp/utils' + +await wait(300) +``` + ### JsonRpcRetryProvider ```js diff --git a/package.json b/package.json index e8848a0..f1cefb3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@instadapp/utils", - "version": "0.1.9", + "version": "0.1.10", "description": "", "repository": "instadapp/utils", "license": "MIT", diff --git a/src/promises/index.ts b/src/promises/index.ts index 891d4d2..cdb3281 100644 --- a/src/promises/index.ts +++ b/src/promises/index.ts @@ -15,12 +15,13 @@ export function retry ( options: RetryOptions, retriesLeft?: number ) { - if (!retriesLeft) { - retriesLeft = options.timeouts.length - } - return new Promise((resolve, reject) => { const { timeouts } = options + + if (typeof retriesLeft === 'undefined' || retriesLeft === null) { + retriesLeft = timeouts.length + } + // Find the timeout for this specific iteration const timeout = timeouts[timeouts.length - retriesLeft] @@ -34,7 +35,7 @@ export function retry ( if (retriesLeft - 1 > 0) { // Delay the new attempt slightly return wait(options.delay || 300) - .then(retry.bind(null, retriesLeft - 1, operation, options)) + .then(retry.bind(null, operation, options, retriesLeft - 1)) .then(resolve) .catch(reject) } @@ -43,3 +44,7 @@ export function retry ( }) }) } + +export { + wait +} diff --git a/test/abi.test.ts b/test/abi.test.ts index 3bbd955..1f095de 100644 --- a/test/abi.test.ts +++ b/test/abi.test.ts @@ -25,7 +25,7 @@ const defaultAbiFetcher = new AbiFetcher({ } }) -describe('abi', () => { +describe.skip('abi', () => { test('can fetch abi', async () => { const abi = await defaultAbiFetcher.get('0x0000000000000000000000000000000000001010', 'polygon') diff --git a/test/cast.test.ts b/test/cast.test.ts index 831e0c3..86ec77f 100644 --- a/test/cast.test.ts +++ b/test/cast.test.ts @@ -36,7 +36,7 @@ beforeAll(async () => { await dsa.setInstance(21723) }) -describe('cast', () => { +describe.skip('cast', () => { test('can get encoded spells from encoded data', () => { const spells = dsa.Spell() diff --git a/test/promises.test.ts b/test/promises.test.ts new file mode 100644 index 0000000..55a6c55 --- /dev/null +++ b/test/promises.test.ts @@ -0,0 +1,28 @@ +import { expect, describe, test } from 'vitest' +import { retry, wait } from '../src' + +describe('promises', () => { + test('retry', async () => { + let called = 0 + async function testfn () { + called++ + await wait(2) + throw new Error('test error') + } + + await expect(retry(testfn, { timeouts: [1, 2, 3], delay: 1 })).rejects.toThrow('test error') + expect(called).toBe(3) + }) + + test('retry', async () => { + let called = 0 + async function testfn () { + called++ + await wait(10) + throw new Error('test error') + } + + await expect(retry(testfn, { timeouts: [1, 2, 3], delay: 1 })).rejects.toThrow('operation timed out') + expect(called).toBe(3) + }) +})