Promise and async await in Javascript
Promises are created in Javascript to prevent the callback hell. But it came with its own problems and complexities. To deal with them, we were introduced to async and await commands. So today, we are going to teach you, how you can create a promise in Javascript, and how to call it using async/await command.
Video tutorial
Basic example of promise with async await
Let’s start with a basic example. We are going to create a simple Javascript function that will return a promise. The promise will call the callback function with a string value.
function doSomething() {
return new Promise(function (callBack) {
callBack("Response")
})
}
To use the async and await commands, we need to create another function that will use these commands.
async function callDoSomething() {
const response = await doSomething()
console.log(response)
}
The function name will be prepended with async and the function that returns a promise i.e. doSomething() will be prepended with await command. In this case, the response variable should print “Response” that was called at line 3 of previous code.
Finally, we simply need to call the callDoSomething() to run the code.
callDoSomething()
Run the code now, and you will see “Response” in your browser console. Try change the string value of callBack parameter and you will see that it will be reflected in response variable in callDoSomething() function.
Practical example
Let’s move to a more practical example. Let’s say you want to call an AJAX request from the promise. And instead of receiving the response in a callback, you want to receive it using async/await command.
So go ahead and change the doSomething() function to the following:
function doSomething() {
return new Promise(function (callBack) {
const ajax = new XMLHttpRequest()
ajax.open("POST", "server.php", true)
ajax.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200) {
callBack(this.responseText)
}
}
}
const formData = new FormData()
ajax.send(formData)
})
}
The code is self-explanatory but if you want to know more about the XMLHttpRequest, we have created a separate detailed tutorial on this.
Then we will create a simple PHP file that will return a simple string as response. Create a new file named server.php and write the following code in it:
<?php
sleep(1);
echo "Response from server";
exit();
We are calling the sleep() function just to give it 1 second delay. If you run the code now, you will start seeing “Response from server” in the browser console.
[wpdm_package id=’1720′]