Loader with ability to cache & retry operations.
const loader = new ScriptLoader();
loader.retry('https://example.com/script.js').then(_ => {
// ...
});
const loader = new ImageLoader();
loader.retry('https://example.com/image.png').then(image => {
// image loaded
});
function action () {
return new Promise((resolve, reject) => {
setTimeout(_ => {
reject( new Error('Cannot load content') );
}, 1000 * Math.random());
});
}
const loader = new RetryOperation();
loader.retry( action, 10 ).then(content => {
// ...
}).catch(error => {
// content cannot be loaded after 10 attempts
});
function action () {
return new Promise(resolve => {
setTimeout(_ => {
resolve({
content: 'some content'
});
}, 1000 * Math.random());
});
}
// 100 is a max number of cache entities
// You can provide a LRU instance instead of number
const loader = new RetryOperationCached( 100 );
// `example_operation_cache_key` - is a key for caching success result
loader.retry( action, 10, 'example_operation_cache_key' ).then(result => {
// `result` now is an object: { item: <your result>, cached: <true/false> }
}).catch(error => {
// content cannot be loaded after 10 attempts
// will not be cached
});
If you try to load content with same cache key
you will get content from the cache.