Created
April 24, 2020 06:45
-
-
Save samzhao/d54aabf45c8bd850449ea35bb6ae5ce8 to your computer and use it in GitHub Desktop.
This file contains 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
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