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

  • Home
  • SEARCH
  • 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 8334179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:15:40+00:00 2026-06-09T03:15:40+00:00

My production code has this chunk of logic, compiled from CoffeeScript: results = [];

  • 0

My production code has this chunk of logic, compiled from CoffeeScript:

results = [];
results.push(slowOpA());
results.push(slowOpB());
results.push(slowOpC());
results = _.flatten(results);
$.when.apply($, results).then(onComplete).fail(onError).always(function() {
    $(document).trigger('stop')
});

I would expect this to first call all the slowOp* methods in sequence, then call either onComplete or onError exactly once, and finally call always exactly once. So that’s what I test for with Jasmine:

@stopSpy = jasmine.createSpy '<stopSpy>'
$(document).bind 'stop', @stopSpy
...
@lastPromise = new $.Deferred()
spyOn('slowOpA').andReturn new $.Deferred().resolve()
spyOn('slowOpB').andReturn new $.Deferred().resolve()
spyOn('slowOpC').andReturn @lastPromise
...
@lastPromise.resolve()
expect(@stopSpy).toHaveBeenCalledOnce()

And 99 out of 100 times that works, but every now and then it gets 2 or 3 calls instead:

Expected spy on <stopSpy> to have been called once, but was called '2' times

Am I missing something? Or is this some obscure race condition in jQuery/Jasmine?

  • 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-09T03:15:42+00:00Added an answer on June 9, 2026 at 3:15 am

    I’m unfamiliar with Jasmine, but your JavaScript code is correct; I mocked up something equivalent in jsFiddle and it worked as expected. I swapped the ‘then(onComplete)’ with ‘done(onComplete)’ and with ‘then(onComplete, onError)’ but all worked as expected. I assumed the slowOpX’s returned deferred/promises. The Underscore (?) _.flatten(results) is redundant but I doubt that’d cause any problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If my Perl code has a production code location and "beta" code location (e.g.
I've first observed this issue in a production code, then made a prototype: import
I regularly see production code from developers (large companies and individuals) that contains code
Ok so i have my users_controller update action with has this code and this
I'm writing some code that has a lot of substitution. There's a list<char> productions
What are your best examples of using Reflection in production code?
What is your opinion of using the infix operator hack in production code? Issues:
I have a solution with two projects. One for project for the production code
OK, so I did the dumb thing and released production code (C#, VS2010) that
I was trying to add some non-production test code by creating a 3rd partial

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.