Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
promiseRetry(promiseFn).then(function (str: string) { console.log(str); });
promiseRetry(promiseFn, {
retries: 1,
factor: 1,
minTimeout: 1000,
maxTimeout: 5000,
randomize: true,
})
.then(function (str: string) {
console.log(str);
});
// $ExpectError
promiseRetry(2);
// $ExpectError
promiseRetry(function () { /* noop */ }, {retries: 'banana'});
// $ExpectError
promiseRetry(promiseFn).then(function (num: number) { console.log(num); });
).catch(err => {
console.error(
'Failed to create adb client using shell adb command. Trying with adbkit.\n' +
err.toString(),
);
/* In the event that starting adb with the above method fails, fallback
to using adbkit, though its known to be unreliable. */
const unsafeClient: Client = adbkit.createClient(adbConfig());
return reportPlatformFailures(
promiseRetry(
(retry, attempt): Promise => {
return unsafeClient
.listDevices()
.then(() => {
return unsafeClient;
})
.catch((e: Error) => {
console.warn(
`Failed to start adb client. Retrying. ${e.message}`,
);
if (attempt <= MAX_RETRIES) {
retry(e);
}
throw e;
});
},
const cacheUrl = uriOptions.uri;
if (defeatBrowserCache) {
uriOptions.uri = addParametersToUrl(uriOptions.uri, `noCache=${Date.now()}`);
}
// If this resource has already been requested, just return the same promise
let promise = requestCache[cacheUrl];
if (promise) {
// console.log(`request serving promise from cache ${cacheUrl}`); // eslint-disable-line
// Remove promise from cache to enable response to be garbage collected
requestCache[cacheUrl] = null;
return promise;
}
promise = PromiseRetry(
retry => {
return promisifiedRequest(requestFunction, uriOptions, retries).catch(retry);
},
{
retries,
minTimeout: 200,
randomize: true
}
);
// Add the new promise to the request cache (if requested)
if (useLocalCache) {
// console.log(`request saving promise in cache ${cacheUrl}`); // eslint-disable-line
requestCache[cacheUrl] = requestCache[cacheUrl] || promise;
}
private loadPeersFromBootstrap() {
// const bootstrapNodesLenght = (this.nodeDiscovery as RadixNodeDiscoveryHardcoded).bootstrapNodes.length;
// if(bootstrapNodesLenght > 1)
// throw new Error('not cool ' + bootstrapNodesLenght)
return promiseRetry(
async (retry, attempt) => {
try {
this.liveNodes = await this.nodeDiscovery.loadNodes()
this.lastNetworkUpdate = Date.now()
return this.liveNodes
} catch (error) {
logger.error(error)
retry()
}
},
{
retries: 1000,
maxtimeout: 60000,
},
)
}
private async retryOnFailure(
operation: () => Promise,
retries = this.maxRetries
): Promise {
const c = console
return promiseRetry(
(retry, n) => {
if (this.closing) {
return Promise.resolve() as any
}
if (n > 1) {
c.error(
`gRPC connection is in failed state. Attempt ${n}. Retrying in 5s...`
)
}
return operation().catch(err => {
// This could be DNS resolution, or the gRPC gateway is not reachable yet
const isNetworkError =
err.message.indexOf('14') === 0 ||
err.message.indexOf('Stream removed') !== -1
if (isNetworkError) {
c.error(`${err.message}`)
_sendEmail(address, subject, message) {
if (this.loggingOnly) {
info(`Sending Email: to=${address} subject=${subject}`);
return Promise.resolve(true);
}
const mailOptions = {
from: 'Kadira Alerts ',
to: address,
subject,
text: fromString(message),
html: message
};
let retryPromise = promiseRetry(retry => {
try {
Fiber(() => {
return Email.send(mailOptions);
}).run();
} catch(error) {
retry(error);
};
}, retryOptions);
return retryPromise
.catch(err => {
error(`Sending email failed: ${err.stack}`);
});
}
async function retryFetch (requestUrl: string, jsonFetchOptions: JsonFetchOptions): Promise {
const shouldRetry = jsonFetchOptions.shouldRetry || DEFAULT_SHOULD_RETRY;
const retryOptions = Object.assign({}, DEFAULT_RETRY_OPTIONS, jsonFetchOptions.retry);
const requestOptions = getRequestOptions(jsonFetchOptions);
try {
const response = await promiseRetry(async (throwRetryError, retryCount) => {
try {
const res = await fetch(requestUrl, requestOptions);
if (shouldRetry(res))
throwRetryError();
return res;
} catch (err) {
err.retryCount = retryCount ? retryCount - 1 : 0;
if (err.code !== 'EPROMISERETRY' && shouldRetry(err))
throwRetryError(err);
throw err;
}
}, retryOptions);
return response;
} catch (err) {
err.name = 'FetchError';
throw err;
promiseRetry(promiseReturningFunc) {
let logger = loggerCreator(this.promiseRetry.name, moduleLogger);
let lastError = null;
return promiseRetryLib((retry, number) => {
logger.info(`start. try number: ${number}`);
return promiseReturningFunc(lastError).catch(err => {
logger.warn(`promise failed... retrying: ${err}`);
lastError = err;
retry(err);
})
}, {retries: 1000})
}
}
const FetchGet = async (uri, options, beforeRetry) =>
await promiseRetry(
(retry, number) =>
fetch(
uri,
merge(
{},
{
method: "GET"
},
CommonOptions,
{
mode: number <= FetchRetryConfig.retries ? "cors" : "no-cors"
},
options
)
).catch(err => {
beforeRetry && beforeRetry(number);
async waitForHealthy(
services?: Array,
progressCallback?: Function,
retryConfig?: RetryConfig = {factor: 1.1, retries: 30, minTimeout: 200}
): Promise {
if (services == null) {
services = (await this.ps())
.filter(service => service && service.raw && service.raw.State.Health)
.map(service => service.name)
}
try {
await retryPromise(
retryConfig,
async (retry) => {
const ps = await this.ps()
const specifiedServices = ps
.filter(service => services && services.indexOf(service.name) !== -1)
const servicesWithoutHealthInfo = specifiedServices
.filter(service => !service.raw || !service.raw.State.Health)
.map(service => service.name)
if (servicesWithoutHealthInfo.length > 0) {
throw new NavyError('The specified services don\'t have health checks: ' + servicesWithoutHealthInfo.join(', '))
}
const serviceHealth = specifiedServices.map(service => ({