-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path33 Asynchronous in js.html
58 lines (52 loc) · 2.87 KB
/
33 Asynchronous in js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html lang="en">
<head><title>Asynchronous in JS</title>
</head>
<body>
<!--
Functions running in parallel with other functions are called asynchronous
A good example is JavaScript setTimeout().
Asynchronous makes js different then other languages.
There are three different types of Asynchronous JS:
1)Callbacks 2)Promises 3)Async/Await(new feature of JS)
In the real world, callbacks are most often used with asynchronous functions.
A typical example is JavaScript setTimeout().
important :
By default, JavaScript is a synchronous, single threaded programming language.
This means that instructions can only run one after another, and not in parallel.
Consider the little code snippet below:
let a = 1;
let b = 2;
let sum = a + b;
console.log(sum);
The above code is pretty simple – it sums two numbers and then logs the sum to the browser
console. The interpreter executes these instructions one after another in that order until it
is done.
But this method comes along with disadvantages. Suppose we wanted to fetch some large amount of
data from a database and then display it on our interface. When the interpreter reaches the
instruction that fetches this data, the rest of the code is blocked from executing until the
data has been fetched and returned.
Now you might say that the data to be fetched isn't that large and it won't take any noticeable
time. Imagine that you have to fetch data is at multiple different points. This delay compounded
doesn't sound like something users would want to come across.
Luckily for us, the problems with synchronous JavaScript were addressed by introducing
asynchronous JavaScript.
Think of asynchronous code as code that can start now, and finish its execution later.
When JavaScript is running asynchronously, the instructions are not necessarily executed
one after the other as we saw before.
visit this website : https://www.freec odecamp.org/news/asynchronous-javascript-explained/
-->
<script>
//Waiting for a Timeout
setTimeout(myfunction,3000);
function myfunction(){
console.log("It took me 3 seconds to display!");
}
console.log("hi");
// Asynchronous programming is a technique that enables your program to start a potentially
// long-running task and still be able to be responsive to other events while that task runs,
// rather than having to wait until that task has finished. Once that task has finished,
// your program is presented with the result.
</script>
</body>
</html>