Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8052725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:37:20+00:00 2026-06-05T07:37:20+00:00

It is the first time I am using $.when and I am having difficulty

  • 0

It is the first time I am using $.when and I am having difficulty with the syntax.
I have code similar to simplified example below. It works (if I haven’t caused an error when I simplified it).
My problem is that I don’t know home many elements the customerIds array would contain.

var customerIds = new [1, 2, 3];

$.when(
    getCustomerData(customerIds[0]),
    getCustomerData(customerIds[1]),
    getCustomerData(customerIds[2])
).then(function() {
    alert('success');
}).fail(function() {
    alert('error');
});

function getCustomerData(int id) {
    return new $.Deferred(function(defer) {
                    doSomeWork(id, defer);
    }).promise();       
}

I would like to write the $.when statement as follows but having difficulty getting the syntax right.

$.when(
    getCustomerDataCalls(customerIds),
).then(function() {
    alert('success');
}).fail(function() {
    alert('error');
});

Where getCustomerDataCalls is implemented as:

function getCustomerDataCalls(customerIds) {
    var dfds = [];

    for (var id in customerIds) {
        dfds.push(new $.Deferred(function(defer) {
                                    doSomeWork(id, defer);
                                 }).promise());     
    }

    return dfds;
}

Unfortunately something is wrong with my implementation and I cannot work out where I am going wrong. My best guess is that something is going wrong when returning an array of Deferreds

Update:
I updated the code after lanzz mentioned that my contrived example already returns a Deferred, I updated my example to include doSomeWork

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:37:22+00:00Added an answer on June 5, 2026 at 7:37 am

    Yes, I have stumbled upon this as well: when does not easily allow to be passed an array. But you could use apply to achieve the desired result.

    $.when.apply($, getCustomerDataCalls(customerIds))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used log4net in winforms before. First time using with a wpf console
This is my first time using a 3 dimensional array and I am having
I'm using the Joda-Time library with Java. I'm having some difficulty trying to turn
This is my first time using EOF and/or files, and I am having an
This is my first time using nginx and I'm having some problems configuring an
I'm using the java.util.concurrency framework for the first time. Here's a very simplified version
I'm using Devise for the first time with rails, and I'm having trouble with
This is my first time ever using sockets, and I am having trouble accepting
I'm having some difficulty using picture boxes as buttons. I'd like to have custom
This is my first time using ICU API, and I'm having a very hard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.