CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
retryify.js47 linesDownload Raw Back to dist
1/* IMPORT */2import RetryfyQueue from './retryify_queue.js';3/* MAIN */4//FIXME: There are a boatload of anys here, but apparently generics cannot be extended properly, so...5const retryifyAsync = (fn, isRetriableError) => {6    return function retrified(timestamp) {7        return function attempt(...args) {8            return RetryfyQueue.schedule().then(cleanup => {9                const onResolve = (result) => {10                    cleanup();11                    return result;12                };13                const onReject = (error) => {14                    cleanup();15                    if (Date.now() >= timestamp)16                        throw error;17                    if (isRetriableError(error)) {18                        const delay = Math.round(100 * Math.random());19                        const delayPromise = new Promise(resolve => setTimeout(resolve, delay));20                        return delayPromise.then(() => attempt.apply(undefined, args));21                    }22                    throw error;23                };24                return fn.apply(undefined, args).then(onResolve, onReject);25            });26        };27    };28};29const retryifySync = (fn, isRetriableError) => {30    return function retrified(timestamp) {31        return function attempt(...args) {32            try {33                return fn.apply(undefined, args);34            }35            catch (error) {36                if (Date.now() > timestamp)37                    throw error;38                if (isRetriableError(error))39                    return attempt.apply(undefined, args);40                throw error;41            }42        };43    };44};45/* EXPORT */46export { retryifyAsync, retryifySync };47 
basant307/AI_Governance_Project · CoolFace