Callbacks and Async Operation
In Javascript the code is executed line-by-line in a
sequence so when we run a parallel operation or asynchronous operation like
fetching data from backend, javascript doesn't waits for the response it
simply executes the next line of code. We give the asynchronous operation a
function to call when it is completed. This function is called a Callback
Function.
await - Asynchronous actions are the action that we
initiate now & they finish later.
Eg - SetTimeout
Synchronous actions are the actions that initiate
& finish one-by-one.
Async - Asynchronous it
allows a program to run a function without stop/hold/freezing the entire
program. This is done using the Async/Await keyword.
Async/Await make it easier to write promises.
The keyword 'async'
before a function makes the function return a promises.
The keyword
await is used inside async keyword.
Introduction to Promises
A promises is used to handle the asynchronous result of an
operation. It defers the execute of a code block until an asynchronous request
is completed. This way, other operations can keep running without
interruption.
A promises has 3 states :
# Pending
- It means the operation is going on.
# Fulfilled - It means
the operation was completed.
# Rejection - It means the
operation did not completed and an error can be thrown.
<script> async function atul() { let promises = new promise((resolve, reject) => { setTimeout(() => resolve("Done"), 3000) }); let result = await promise; alert(Result); console.log("The result is ", result); } atul(); </script>
Disclaimer
All tutorials are for informational and educational
purposes only and have been made using our own routers, servers, websites and
other vulnerable free resources. we do not contain any illegal activity. We
believe that ethical hacking, information security and cyber security should
be familiar subjects to anyone using digital information and computers.
Hacking Truth is against misuse of the information and we strongly suggest
against it. Please regard the word hacking as ethical hacking or penetration
testing every time this word is used. We do not promote, encourage, support or
excite any illegal activity or hacking.
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.