Asynchronous JavaScript — Callbacks & Promises

WHAT IS ASYNCHRONOUS JS?

<script>        function getValue(){
let value=0;

setTimeout(function(){
value = 10;
},1000);
return value;
}
console.log(getValue());

</script>

There are mainly 3 types of asynchronous code styles in JavaScript; They are callbacks, promises, and async/await.

Here we are going to get to know about callbacks and promises…

1.Callback Function

<script>
function getValue(callback){
let value = 0;

setTimeout(function(){
value = 10;
callback(value);
},1000);
return value;
}

getValue(function(value){
console.log(value);
});
</script>

Callback Hell??🧐🤔

2.Promise Function

<script>
let promise = new Promise((resolve, reject)=>{
resolve();
reject();
});

function getValue(){
let value = 0;
return new Promise((resolve, reject)=>{
setTimeout(function(){
value = 10;
resolve(value);
},1000);
});
}

getValue().then(function(value){
console.log(value);
});


</script>

Conclusion

We learned a bit of Asynchronous JavaScript and some of its concepts.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store