An hour in the presence of Kelly Sotherton

This piece is a record of my speedily scripted notes from an hour listening to a voice of truth. The sharing of experiences as they happened, how they happened from ‘an in the moment’ perspective. A…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




All Need to Know About JavaScript Promises

A promise is an asynchronous action that may complete at some point in the future and produce a value. This value is not necessarily known at the time of its creation. Once the value is produced, it notifies the user.

Promises provide a robust way to wrap the result of asynchronous work, overcoming the problem of deeply nested callbacks.

A JavaScript promise is created with the help of the new keyword. Here’s the general syntax:

{

// Perform some work

})

The Promise object takes a callback function as a parameter, which, in turn, takes two parameters, resolve and reject. The promise is either fulfilled or rejected.

There are three states governing promises:

It’s known that if a promise is fulfilled, then some piece of code must be executed. And if it’s rejected, then error handling must be performed. So for this, there are two methods:

1. then(callback) is invoked to attach a callback when the promise is resolved/fulfilled.

.this(function(result){

// handle success }

2. catch(callback) method is invoked to attach a callback when the promise is rejected.

.catch(function(error){

//handle error }

Once a promise is settled (either fulfilled or rejected), it’s said to be immutable and cannot change its state.

To help you understand the concept better, see a pictorial representation of promises below:

Now that you know what JavaScript promises are and their general syntax, let’s walk through a simple demo.

<script>

let car = new Promise(function(resolve,reject){

resolve()

else

reject()

});

car.then(function(){

document.write(“The fuel tank is full. Happy Driving!!”)

}).catch(function(){

document.write(“The fuel tank is not empty.”)

})

</script>

In the above code, we’re checking the car’s fuel. The callback function checks for the full condition on the fuel tank. If it’s true, the Promise is fulfilled, and the then() method is invoked. If not, the error is caught by the catch() method.

JavaScript Promises

Now that you know how promises work, let’s show how nested promises work.

In this case, the value of a promise depends on the value of other promises.

Consider a code that checks for the scenario when the fuel tank is empty. If the case is true, then you check if the engine is heating. If this holds as well, you can display a warning message and finally end the promise.

return new Promise(function(resolve,reject){

resolve(“The car doesn’t have enough fuel.”)

})

}

Similarly, here are two more functions, engine and travel, that return a Promise:

let engine = function(msg){

return new Promise(function(resolve,reject){

resolve(msg + “The engine is over heating.”)

})

}

let travel = function(msg){

return new Promise(function(resolve,reject){

resolve(msg + “The car is not safe for driving.”)

})

}

To perform the actions, we use the then method.

return engine(result)

}).then(function(result){

return travel(result)

}).then(function(result){

console.log(“Done!” + result)

})

Here, we’ve returned the function engine. The engine function now returns another promise over which the then method is run again. Within the callback function, we’ve returned the last function, travel.

Now we run the then method on the promise that’s returned to display a message indicating that all the three promises are resolved.

Since every promise is displaying a message, we can catch the message as a result and pass the value to the following functions. We’ve also appended the message in the promises.

When the above code is run, the following output is seen:

Add a comment

Related posts:

S3A on Spark 3.3 in 2023

Updating my post from almost 3 years ago! The world has moved on to Spark 3.3, and so have the necessary JARs you will need to access S3 from Spark.