I need a method to fetch different scripts with a callback. This method works ok:
fetchScripts:function() {
var _this=this;
$.when(
$.ajax({
url:_this.url + 'library/script-one.js',
type:'get',
cache:true
}),
$.ajax({
url:_this.url + 'library/script-two.js',
type:'get',
cache:true
}),
{ .... },
$.ajax({
url:_this.url + 'library/script-n.js',
type:'get',
cache:true
})
).then(function() {
console.log('fetch is done');
})
},
But i'd like to more generalize the method because redundany is increasing. Is it possible to pass a promise to $.when()? Below my first attempt - but url is always the same, i.e. 'script-n.js' Maybe i missed the point and you could illustrate a more "beauty" solution
fetchScripts:function() {
this.deferred=new $.Deferred();
this.promise=this.deferred.promise();
var _this=this;
$.each([
'script-one.js',
'script-two.js',
( .... ),
'script-n.js'
],function() {
_this.script=this;
_this.promise.then(function(){
return $.ajax({
url:_this.url + 'library/' + _this.script,
type:'get',
cache:true
})
});
});
$.when(
this.promise
).then(function() {
console.log('fetch is done');
});
this.deferred.resolve();
},
Deferred() A factory function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.
A promise represents a value that is not yet known. This can better be understood as a proxy for a value not necessarily known when the promise is created. A deferred represents work that is not yet finished. A deferred (which generally extends Promise) can resolve itself, while a promise might not be able to do so.
promise() will attach the methods onto it and then return this object rather than create a new one. This can be useful to attach the Promise behavior to an object that already exists. If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point.
The defer attribute is a boolean attribute. If the defer attribute is set, it specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing. Note: The defer attribute is only for external scripts (should only be used if the src attribute is present).
You still need $.when. But instead, create an array of Deferreds (or promises) and then apply it to $.when:
fetchScripts:function() {
var base = this.url;
var defaults = {
type:'get',
cache:true
};
var libraries = [
'library/script-one.js',
'library/script-two.js',
'library/script-n.js'
];
var deferreds = $.map(libraries, function(current) {
var ajaxOptions = $.extend({ url: base + current }, defaults);
return $.ajax(ajaxOptions);
});
$.when.apply($, deferreds).then(function() {
console.log('All done');
});
}
Alternatively to extending the default settings, you could just use $.ajax(defaults).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With