Hi All,
I would like to take this opportunity and explain you the asynchronous programming in JavaScript.
Before diving deep into asynchronous concepts like callbacks, promises, async/await.
Javascript is a synchronous single threaded programming language – JavaScript (JS) Engine will execute one task at a time.
JavaScript is also a interpreted programming language – JS Engine executes the code line by line from top to bottom.
So, based on above two sentences we cannot run different task in parallel. Isn’t it? Is it good or bad? If you take other languages like Java or .net we have something called multi threading.
No friends, even in JavaScript we can achieve asynchronous behaviour with the help of callbacks and promises.
callbacks – in simple terms : calling back whenever time comes.
Similarly in JavaScript, if a function “A” gets called by function “B”, somehow we must give the function “A” control to a function “B”.
How to achieve it in JS?
Pass function A as an argument to the function B and function B will take care of function A. That’s all you have to do! π
Sample Code Example:
function A(){ console.log("Function A called!") } function B(funcA){ console.log("Inside Function B") funcA() // function A gets called here! } //calling or invoking function B B(A)
In the above code, we have given complete control of function A to function B.
We will go to next level to understand asynchronous with the help of callbacks. I would strongly say that, in JavaScript asynchronous behaviour exist only because of callbacks!!!
console.log("Start") setTimeout(function (){ console.log("setTimeout Called!"), 5000 } console.log("End")
setTimeout is one of the web APIs in JavaScript which takes two arguments 1. callback function and 2. Delay time
When JS engine starts, it goes line by line. So,
First it ogs “Start” in the console.
Then it registers a setTimeout web API in the browser and continues to execute next statements.
Then it logs “End” in the console.
Finally, JS engine waits for the timer (5 sec) to expire then it execute the callback function to log “setTimeout Called!”
This is how we can achieve asynchronous programming in JavaScript.
Continue to this… I will come up with promises and async/await concepts. Stay tuned!
Keep learning and keep sharing π
Cheers,
Adarsha π
47 Comments
ΠΏΒ»ΡFarmacia online migliore: Farma Prodotti – farmacia online piΠβ conveniente
comprare farmaci online all’estero
alternativa al viagra senza ricetta in farmacia viagra generico le migliori pillole per l’erezione
viagra generico in farmacia costo: viagra originale in 24 ore contrassegno – viagra prezzo farmacia 2023
farmacie online sicure
Some casinos have luxurious spa facilities.: taya777.icu – taya777 register login
Online gaming is also growing in popularity. https://winchile.pro/# Los casinos garantizan una experiencia de calidad.
Players often share tips and strategies.: phmacao.life – phmacao casino
taya365 login taya365 login High rollers receive exclusive treatment and bonuses.
http://jugabet.xyz/# Los casinos garantizan una experiencia de calidad.
Casinos offer delicious dining options on-site.
Many casinos offer luxurious amenities and services.: phtaya – phtaya
La pasiΠΡn por el juego une a personas.: jugabet chile – jugabet.xyz
Many casinos offer luxurious amenities and services.: taya365 – taya365
Poker rooms host exciting tournaments regularly. http://taya777.icu/# Cashless gaming options are becoming popular.
https://phtaya.tech/# Gambling can be a social activity here.
Cashless gaming options are becoming popular.
phmacao phmacao com Promotions are advertised through social media channels.
Promotions are advertised through social media channels.: phtaya login – phtaya casino
Los casinos celebran festivales de juego anualmente.: jugabet casino – jugabet casino
http://phtaya.tech/# Live dealer games enhance the casino experience.
Players can enjoy high-stakes betting options.
Live dealer games enhance the casino experience. http://taya777.icu/# The Philippines has a vibrant nightlife scene.
Many casinos host charity events and fundraisers.: phtaya casino – phtaya.tech
The casino industry supports local economies significantly.: taya365.art – taya365
Many casinos offer luxurious amenities and services. https://taya365.art/# Gambling can be a social activity here.
La adrenalina es parte del juego.: jugabet – jugabet chile
https://taya365.art/# Most casinos offer convenient transportation options.
Gambling can be a social activity here.
High rollers receive exclusive treatment and bonuses. http://phmacao.life/# Slot machines feature various exciting themes.
https://jugabet.xyz/# Los jugadores pueden disfrutar desde casa.
Online gaming is also growing in popularity.
Las reservas en lΠΒnea son fΠΠciles y rΠΠpidas.: winchile – winchile.pro
phtaya casino phtaya Loyalty programs reward regular customers generously.
https://phmacao.life/# Poker rooms host exciting tournaments regularly.
Players can enjoy high-stakes betting options.
Poker rooms host exciting tournaments regularly.: taya365 login – taya365.art
Online gaming is also growing in popularity.: taya365 – taya365
https://jugabet.xyz/# Las ganancias son una gran motivaciΠΡn.
Many casinos provide shuttle services for guests.
The poker community is very active here.: taya365.art – taya365
taya365 login taya365 com login The ambiance is designed to excite players.
http://phtaya.tech/# Live dealer games enhance the casino experience.
Manila is home to many large casinos.
Entertainment shows are common in casinos.: taya365.art – taya365
Security measures ensure a safe environment. https://phmacao.life/# Casinos offer delicious dining options on-site.
The casino industry supports local economies significantly.: taya365.art – taya365.art
https://phtaya.tech/# Many casinos provide shuttle services for guests.
The thrill of winning keeps players engaged.
Las promociones de fin de semana son populares.: winchile casino – win chile
Players must be at least 21 years old. http://taya777.icu/# Casino visits are a popular tourist attraction.
Los bonos de bienvenida son generosos.: winchile.pro – winchile.pro
Los jugadores pueden disfrutar desde casa.: winchile.pro – win chile
https://taya365.art/# Players often share tips and strategies.
Casinos offer delicious dining options on-site.
La adrenalina es parte del juego.: winchile casino – win chile
Algunos casinos tienen programas de recompensas.: winchile.pro – win chile
http://phmacao.life/# Entertainment shows are common in casinos.
Visitors come from around the world to play.
taya777 taya777 app Many casinos have beautiful ocean views.