Skip to content

Instantly share code, notes, and snippets.

@osamaishtiaq
Created April 15, 2022 00:14
Show Gist options
  • Save osamaishtiaq/302e619a39a3a84d40bc2877f4ddfe0d to your computer and use it in GitHub Desktop.
Save osamaishtiaq/302e619a39a3a84d40bc2877f4ddfe0d to your computer and use it in GitHub Desktop.
Example of using request retry
// 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