Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fetchWithPromise = (() => {
- let promiseCache = {};
- return function (url, timeout = 2000) {
- return promiseCache[url] = promiseCache[url] || (() => {
- var timer = setTimeout(function () {
- return reject(new Error('Request timed out'));
- }, timeout);
- return Promise.race([timer, simpleCache[url] = fetch(url)
- .then(response => response.text())
- ]);
- })();
- }
- })();
- // Demo Plugin
- $.fn.myPlugin = function(url) {
- var userdata = fetchWithPromise(url, 2000);
- this.each(function(e) {
- userdata.then(function (data) {
- this.innerHTML = data;
- });
- });
- return this;
- };
- // Demo Init Code
- $(function() {
- $(".element-1").myPlugin('https://raw.githubusercontent.com/callaginn/data/master/array.json');
- $(".element-2").myPlugin('https://raw.githubusercontent.com/callaginn/data/master/array.json');
- });
Add Comment
Please, Sign In to add comment