Created
April 15, 2022 00:14
-
-
Save osamaishtiaq/302e619a39a3a84d40bc2877f4ddfe0d to your computer and use it in GitHub Desktop.
Example of using request retry
This file contains hidden or 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
// without request retry | |
async fetchHttpMediaBufferArray(imageUrl) { | |
try { | |
// your request code | |
const bufferResp = await firstValueFrom( | |
this.httpService.get(imageUrl, { responseType: 'arraybuffer' }), | |
); | |
const contentType = bufferResp.headers['content-type']; | |
const buffer = Buffer.from(bufferResp.data); | |
return { buffer, contentType }; | |
} catch (err) { | |
// do something with the err i.e. | |
console.log(err); | |
} | |
} | |
// with request retry | |
async fetchHttpMediaBufferArray(imageUrl) { | |
// just wrap your code in the method | |
// and pass error handling code as second parameter | |
return requestWithRetry( | |
async () => { // your request code | |
const bufferResp = await firstValueFrom( | |
this.httpService.get(imageUrl, { responseType: 'arraybuffer' }), | |
); | |
const contentType = bufferResp.headers['content-type']; | |
const buffer = Buffer.from(bufferResp.data); | |
return { buffer, contentType }; | |
}, | |
err => { // your error handling code as 2nd parameter | |
console.log(err); | |
}, | |
5 // optional, number of retries before it finally fails and executes your error handler code | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment