Gerfried Gerfried - 12 days ago 6
Javascript Question

jQuery promise (variable number of promises)

I have a script that loads multiple SVG files and should then draw them.

var s = Snap("#svg");
var bigCircle =, 150, 100);

// Helper to convert Snap.load() into a Promise.
function loadSVG(url) {
var deferred = new $.Deferred();
Snap.load(url, function(x) {
deferred.resolve( x );
return deferred.promise();

// Make an array of Promises.
var loadPromises = [

// Wait for all the Promises to finish.
$.when( loadPromises ).done(function ( results ) {
console.log(results); //<-- seems to be a promise again!!!!
for (var i = 0; i < results.length; ++i) {
var svg = results[i];
// Your processing of each SVG goes here.
var g ="g");

I don't get any errors, but the strange thing is, that my done() result seems to be a promise again (at least it looks like that in the console). The result of done() should be an array of SnapJs objects. What am I doing wrong?


You'll have to use apply to pass in an array of arguments to $.when

$.when.apply($, loadPromises).done(function () {
    var results = [];


This then equals

$.when(loadPromises[0], loadPromises[1], loadPromises[2], etc)