Contents
- 1 What does it mean to chain promises in JavaScript?
- 2 Which is an example of optional chaining in JavaScript?
- 3 How to make the chain extendable in JavaScript?
- 4 Do you need multiple handlers for one promise?
- 5 What happens when a handler returns a promise?
- 6 When do you use chaining in JavaScript code?
- 7 How are promise chains great at error handling?
What does it mean to chain promises in JavaScript?
Promises chaining. The value returned by .then is a promise, that’s why we are able to add another .then at (2). When the value is returned in (1), that promise becomes resolved, so the next handler runs with the value.
Which is an example of optional chaining in JavaScript?
The optional chaining ?. is a safe way to access nested object properties, even if an intermediate property doesn’t exist. If you’ve just started to read the tutorial and learn JavaScript, maybe the problem hasn’t touched you yet, but it’s quite common. As an example, let’s say we have user objects that hold the information about our users.
What happens when a promise is returned in JavaScript?
When a handler returns a value, it becomes the result of that promise, so the next .then is called with it. A classic newbie error: technically we can also add many .then to a single promise. This is not chaining. What we did here is just several handlers to one promise.
How to make the chain extendable in JavaScript?
To make the chain extendable, we need to return a promise that resolves when the avatar finishes showing. That is, the .then handler in line (*) now returns new Promise, that becomes settled only after the call of resolve (githubUser) in setTimeout (**). The next .then in the chain will wait for that.
Do you need multiple handlers for one promise?
In practice we rarely need multiple handlers for one promise. Chaining is used much more often. A handler, used in .then (handler) may create and return a promise. In that case further handlers wait until it settles, and then get its result.
When to use promises in frontend programming?
In frontend programming promises are often used for network requests. So let’s see an extended example of that. We’ll use the fetch method to load the information about the user from the remote server. It has a lot of optional parameters covered in separate chapters, but the basic syntax is quite simple:
What happens when a handler returns a promise?
If a .then (or catch/finally, doesn’t matter) handler returns a promise, the rest of the chain waits until it settles. When it does, its result (or error) is passed further. Are these code fragments equal? In other words, do they behave the same way in any circumstances, for any handler functions?
When do you use chaining in JavaScript code?
So in the code above all alert show the same: 1. In practice we rarely need multiple handlers for one promise. Chaining is used much more often. A handler, used in .then (handler) may create and return a promise. In that case further handlers wait until it settles, and then get its result.
When to use a promise in Salesforce Lightning?
Promise.then () takes two functions as parameters – the first is a success callback, invoked if and when the promise is resolved, and the second is an error callback, invoked if and when the promise is rejected. When building Lightning Components, asynchronous interaction with the Salesforce server is typically carried out via an action.
How are promise chains great at error handling?
Promise chains are great at error handling. When a promise rejects, the control jumps to the closest rejection handler. That’s very convenient in practice. For instance, in the code below the URL to fetch is wrong (no such site) and .catch handles the error: