Facebook Pixel
Promise Chaining: A Deep Dive in JavaScript

What happens if a .then callback returns a value in JavaScript?

The answer

The next .then receives that value. For example, Promise.resolve(5).then(n => n + 1).then(n => console.log(n)) logs 6. Each value is passed to the next .then.

More FAQs in Promise Chaining: A Deep Dive in JavaScript

Each .then returns a new promise. If the callback returns a value, the new promise fulfills with it. If it returns a promise, the new promise waits for it. The next .then receives the result. This lets you sequence async operations flatly.

The next .then waits for the returned promise to settle. The next .then receives the resolved value. This is how you chain async operations that depend on each other.

Return an object or array from each .then: .then(user => fetchPosts(user).then(posts => ({ user, posts }))). Or use async/await with local variables, which is cleaner and avoids this problem.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.