Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

When

The $.when() method

Now, using our test page as an example, I'll show you the nifty $.when() method:

$.when(
  $("article h3").hide(3000),
  $("article img").slideUp(1000),
  $("article p").fadeTo(2000, 0.1)
).then(() =>
  $("article").slideUp(500)
).done(() =>
  console.info("done")
).catch(() =>
  console.error("fail :(")
)

Let me explain what's happening — all animations start simultaneously, and when they all finish, the function we pass as an argument to the then() method gets called (one of two, depending on the outcome).

To make this "magic" work, $.when(), $.ajax(), and animate() all implement the Deferred interface. That's why we can use all the methods we explored in the previous chapter.

And now for the fancy version — the when() method returns a projection of a Deferred object, accepts an arbitrary number of Deferred objects as parameters, and when all of them have completed, the when object changes its state to "resolved", triggering all subscribers.

Last updated