0

I'm using jquery and have two variables.

trackFeatures - single ajax request, artistRequests - array of ajax requests

I want to do something when both trackFeatures and artistRequests have completed. For just artistRequests, I can do:

$.when.apply($, artistRequests).done(function() { ... }

Is there a way to combine the single request with the array requests? Besides adding the single request to the array or testing them sequentially.

Thanks

1 Answer 1

1

Referring this link and example :

var d1 = $.Deferred();
var d2 = $.Deferred();
 
$.when( d1, d2 ).done(function ( v1, v2 ) {
    console.log( v1 ); // "Fish"
    console.log( v2 ); // "Pizza"
});
 
d1.resolve( "Fish" );
d2.resolve( "Pizza" );

Similarly if you have array of promises like artistRequests = [d1,d2]

You can use spread operator for array like

$.when(trackFeatures , ...artistRequests ).done(function ( v1, v2 ) {
    console.log( v1 ); // "Fish"
    console.log( v2 ); // "Pizza"
});

PS: ...artistRequests split the each value of array in single variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like I can just use the ...artistRequests as if it was its own argument, so $.when(trackFeatures, ...artistRequests).done(function() { ... } is working for me, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.