-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.js
71 lines (57 loc) · 1.48 KB
/
testing.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
function someFunction(){
let start = Date.now();
while (Date.now() - start < 500) {
//1000 nanoseconds = 1 second
// do nothing
}
return "Hello";
}
console.log('Starting...');
let result = someFunction();
console.log(result);
console.log('...Finishing');
//////////////below is the example for asynchrounous programming////////////////
console.log("Start of script");
setTimeout( () => {
console.log("First timeout completed");
}, 1000);
console.log("End of script");
//callback example///
// Declare function
function fetchData(callback) {
setTimeout(() => {
const data = {name: "John", age: 30};
callback(data);
}, 3000);
}
// Execute function with a callback
fetchData(function(data) {
console.log(data);
});
console.log("Data is being fetched...");
//promise//
// Initialize a promise
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Hello from the promise!");
}, 5000);
});
console.log(myPromise)
myPromise
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
})
.finally(() => {
//code here will be executed regardless of the status
//of a promise (fulfilled or rejected)
console.log("this is the final state")
});
async function getData() {
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const data = await response.json();
console.log(data);
}
getData();