SyntaxStudy
Sign Up
jQuery Transforming Results with .then()
jQuery Intermediate 4 min read

Transforming Results with .then()

Transforming with .then()

The value returned from a .then() callback becomes the resolved value for subsequent callbacks — use it to transform or extract data from responses.

Example
$.getJSON("/api/users")
  .then(function(users) {
    // Transform: return just the names
    return users.map(u => u.name);
  })
  .then(function(names) {
    // names is now ["Alice", "Bob", ...]
    console.log("User names:", names);
    return names.length;
  })
  .then(function(count) {
    console.log("Total users:", count);
  });
Pro Tip

Returning a non-Deferred value from .then() wraps it in a resolved Promise automatically.