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.
The value returned from a .then() callback becomes the resolved value for subsequent callbacks — use it to transform or extract data from responses.
$.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);
});
Returning a non-Deferred value from .then() wraps it in a resolved Promise automatically.