Skip to content

Instantly share code, notes, and snippets.

@samzhao
Created April 24, 2020 06:45
Show Gist options
  • Save samzhao/d54aabf45c8bd850449ea35bb6ae5ce8 to your computer and use it in GitHub Desktop.
Save samzhao/d54aabf45c8bd850449ea35bb6ae5ce8 to your computer and use it in GitHub Desktop.
import isEmpty from 'lodash/isEmpty'
const retryIfEmpty = async (input): Promise<any> => {
const { request, timeout = 200, maxCalls = 6 } = input;
return new Promise(async (resolve, reject) => {
try {
const { data } = await request;
if (isEmpty(data)) {
retryIfEmpty({ ...input, maxCalls: maxCalls - 1 });
await new Promise((r) => setTimeout(r, timeout));
} else {
resolve(data);
}
} catch (err) {
reject(err);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment