Promises in JavaScript are a general object of this programming language that They allow us to prepare different actions to be executed in different scenarios. Additionally, this object allows us to create a scenario in which an error is executed, that is, the promise is rejected. However, for our project to work, we cannot leave a bug untreated. Therefore, in this post, we will teach you What is flow control and how does it work? try catch in JavaScript to handle errors in a promise.
What is try catch flow control in JavaScript?
As you have read in our post about creating a promise in JavaScript with Math.random, in this programming language we can cause a promise to be resolved or rejected based on a random value. Besides, By playing with the value of each condition, we can make it more or less likely that a promise will resolve.
In the following lines of code, we define that a promise resolves whenever there is a value greater than 0.1:
let promise = new Promise ((resolve, reject) => {
if (Math.random () > 0.1) {
resolve(‘greater than 0.1’)
} else {
reject (‘less than 0.1’)
})
console.log (promise)
Since the scale of the method Math.random goes from 0.0 to 0.9, the promise is more likely to be resolved than to fail. However, it can still fail, and we must be prepared for when it does. For this, flow control is used try catch in JavaScript.
Flow control try catch in JavaScript is a construct that allows us to handle the failure of a promise. This construction is written as follows:
try {
} catch (error) {
}
As you can see in the previous lines of code, the key word catch needs a parameter: the error. For its part, the keyword try It does not require any parameters. This flow control is equivalent to conditional if…else. This is because, inside the keys of the try, We will define what happens in the event that everything goes well (if). Then, in the code catch, we will have what should happen when the code of the try may give an error. This section serves a similar objective to the keyword else.
So, to exemplify this control flow, let’s fill in its lines of code. In this case, we will use the promise properties promise which we have created before using the method Math.random.
Normally we use the symbols ${variable} to call the variables or properties of an object in the context of a string inside the command console.log. However, using them here simply returns us object promise. Therefore, we use another syntax:
try {
console.log (‘The promise works’, promise)
} catch (error){
console.log (‘Promise doesn’t work’, error)
}
With this flow control we will see that when the promise works (that is, it goes through resolve)we get the string greater than 0.1. However, In cases where it doesn’t work, we get an error.
Another way to catch errors and control what happens when they appear in a promise is to use the methods then and catch of JavaScript. So, we can write the same result of control flow try catch with these methods. Below, we show you the lines of code that lead us to this:
promise.then(response => {
console.log (‘The promise works’, promise)
}).catch(error => {
console.log (‘Promise doesn’t work’, error)
})
In this case, we have decided to use the way of writing in which we concatenate the methods then and catch. That is, the method .catch you are receiving the result of the method .then and relies on it to execute its action. This way of writing is often more efficient than flow control. try catch and even that the other way of writing methods, since It not only captures the error, but resolves it immediately.
Do you dare to continue learning?
Now that you know how to catch an error using control flow try catch in JavaScript and other methods, Surely you want to continue learning about the possibilities of this programming language! To do this, we recommend taking a look at our Full Stack Web Development Bootcamp, a comprehensive and intensive training in which you will learn everything you need to master JavaScript and other programming languages for the web, such as CSS and HTML. Do you dare to continue learning with us? We will wait for you!