diff --git a/dist/cjs/index.js b/dist/cjs/index.js index c29e3f6..f2ad45e 100644 --- a/dist/cjs/index.js +++ b/dist/cjs/index.js @@ -63,9 +63,11 @@ let cache = function ({ call, key, expires = 0 }) { // get existing promise (of a previous pending request asking for the exact same thing) let existingPromise = getPromise({ key }); - if(existingPromise) { return existingPromise.then((value)=>{ - return resolve(value) - }) } + if(existingPromise) { + return existingPromise + .then(resolve) + .catch(reject) + } setPromise({ key, promise: new Promise((resolveQueue, rejectQueue)=>{ if (expires === 0) { @@ -104,6 +106,8 @@ let cache = function ({ call, key, expires = 0 }) { }) }).then(()=>{ deletePromise({ key }); + }).catch(()=>{ + deletePromise({ key }); }); }) }; diff --git a/dist/es/index.js b/dist/es/index.js index fc73b16..8a3351d 100644 --- a/dist/es/index.js +++ b/dist/es/index.js @@ -59,9 +59,11 @@ let cache = function ({ call, key, expires = 0 }) { // get existing promise (of a previous pending request asking for the exact same thing) let existingPromise = getPromise({ key }); - if(existingPromise) { return existingPromise.then((value)=>{ - return resolve(value) - }) } + if(existingPromise) { + return existingPromise + .then(resolve) + .catch(reject) + } setPromise({ key, promise: new Promise((resolveQueue, rejectQueue)=>{ if (expires === 0) { @@ -100,6 +102,8 @@ let cache = function ({ call, key, expires = 0 }) { }) }).then(()=>{ deletePromise({ key }); + }).catch(()=>{ + deletePromise({ key }); }); }) }; diff --git a/dist/umd/index.js b/dist/umd/index.js index 53a9317..40c2fed 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -62,9 +62,11 @@ // get existing promise (of a previous pending request asking for the exact same thing) let existingPromise = getPromise({ key }); - if(existingPromise) { return existingPromise.then((value)=>{ - return resolve(value) - }) } + if(existingPromise) { + return existingPromise + .then(resolve) + .catch(reject) + } setPromise({ key, promise: new Promise((resolveQueue, rejectQueue)=>{ if (expires === 0) { @@ -103,6 +105,8 @@ }) }).then(()=>{ deletePromise({ key }); + }).catch(()=>{ + deletePromise({ key }); }); }) }; diff --git a/package.json b/package.json index 2edfead..4e90620 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "depay-web3-client", "moduleName": "Web3Client", - "version": "4.0.1", + "version": "4.0.2", "description": "A web3 client to fetch blockchain data just like we are used to with http clients.", "main": "dist/cjs/index.js", "module": "dist/es/index.js", diff --git a/src/cache.js b/src/cache.js index 425608d..3b4f020 100644 --- a/src/cache.js +++ b/src/cache.js @@ -56,9 +56,11 @@ let cache = function ({ call, key, expires = 0 }) { // get existing promise (of a previous pending request asking for the exact same thing) let existingPromise = getPromise({ key }) - if(existingPromise) { return existingPromise.then((value)=>{ - return resolve(value) - }) } + if(existingPromise) { + return existingPromise + .then(resolve) + .catch(reject) + } setPromise({ key, promise: new Promise((resolveQueue, rejectQueue)=>{ if (expires === 0) { @@ -97,6 +99,8 @@ let cache = function ({ call, key, expires = 0 }) { }) }).then(()=>{ deletePromise({ key }) + }).catch(()=>{ + deletePromise({ key }) }) }) } diff --git a/tests/units/bsc/request/cache.spec.js b/tests/units/bsc/request/cache.spec.js index c84448b..98bdb3f 100644 --- a/tests/units/bsc/request/cache.spec.js +++ b/tests/units/bsc/request/cache.spec.js @@ -82,6 +82,17 @@ describe('request cache on bsc', () => { expect(decimalMock3).toHaveBeenCalledTimes(1) expect(decimalMock4).toHaveBeenCalledTimes(1) }) + + it('serves responses correctly even if some of them fail', async ()=>{ + + let decimalMock1 = mock({ blockchain: 'bsc', call: { to: '0x6b175474e89094c44da98b954eedeac495271d0f', api: Token['bsc'].DEFAULT, method: 'decimals', return: Error('something went wrong') } }) + let decimalMock2 = mock({ blockchain: 'bsc', call: { to: '0xdac17f958d2ee523a2206206994597c13d831ec7', api: Token['bsc'].DEFAULT, method: 'decimals', return: '6' } }) + + request({ blockchain: 'bsc', address: '0x6b175474e89094c44da98b954eedeac495271d0f', method: 'decimals' },{ api: Token['bsc'].DEFAULT, cache: 86400000 }) + .then(()=>{}) + .catch(()=>{}) + await request({ blockchain: 'bsc', address: '0xdac17f958d2ee523a2206206994597c13d831ec7', method: 'decimals' },{ api: Token['bsc'].DEFAULT, cache: 86400000 }) + }) it('it caches results until they expire on bsc', async ()=> { diff --git a/tests/units/ethereum/request/cache.spec.js b/tests/units/ethereum/request/cache.spec.js index 024768a..ad66fba 100644 --- a/tests/units/ethereum/request/cache.spec.js +++ b/tests/units/ethereum/request/cache.spec.js @@ -82,6 +82,17 @@ describe('request cache on ethereum', () => { expect(decimalMock3).toHaveBeenCalledTimes(1) expect(decimalMock4).toHaveBeenCalledTimes(1) }) + + it('serves responses correctly even if some of them fail', async ()=>{ + + let decimalMock1 = mock({ blockchain: 'ethereum', call: { to: '0x6b175474e89094c44da98b954eedeac495271d0f', api: Token['ethereum'].DEFAULT, method: 'decimals', return: Error('something went wrong') } }) + let decimalMock2 = mock({ blockchain: 'ethereum', call: { to: '0xdac17f958d2ee523a2206206994597c13d831ec7', api: Token['ethereum'].DEFAULT, method: 'decimals', return: '6' } }) + + request({ blockchain: 'ethereum', address: '0x6b175474e89094c44da98b954eedeac495271d0f', method: 'decimals' },{ api: Token['ethereum'].DEFAULT, cache: 86400000 }) + .then(()=>{}) + .catch(()=>{}) + await request({ blockchain: 'ethereum', address: '0xdac17f958d2ee523a2206206994597c13d831ec7', method: 'decimals' },{ api: Token['ethereum'].DEFAULT, cache: 86400000 }) + }) it('it caches results until they expire on ethereum', async ()=> {