Javascript Day-3
Javascript Day-3
Javascript Day-3
greet();
greet('John');
............................................
The rest operator is denoted by three dots (...) and is used in function parameter
lists to capture multiple arguments into a single array. It allows you to represent
an indefinite number of arguments as an array within a function.
function sum(...numbers) {
return numbers.reduce((total, number) => total + number, 0);
}
console.log(sum(1, 2, 3, 4));
...........................................
The spread operator, also denoted by three dots (...), is used to expand elements
from an array or object into individual elements. It can be used in function calls,
array literals, and object literals.
......................................................
Synchronous Execution:
In synchronous execution, code is executed line by line, and each operation blocks
the execution until it completes.
This means that each operation must finish before the next one can begin.
Synchronous operations are typically predictable and easy to understand, but they
can also cause the program to halt if an operation takes a long time to complete.
console.log("Start");
console.log("First");
console.log("Second");
console.log("End");
............................................................
Asynchronous Execution:
console.log("Start");
setTimeout(function() {
console.log("Async operation complete");
}, 2000);
console.log("End");
................................
How many ways to achive acynchronous exection
callback:
function fetchData(callback) {
const data = "Async data";
callback(null, data);
}
fetchData(handleData);
console.log("Vinay");
console.log("namith");
console.log("sashank");
.............................................
Promise:
A Promise in JavaScript represents the eventual completion (or failure) of an
asynchronous operation. It is an object that may produce a single value in the
future. Promises help handle asynchronous code in a more structured and readable
way.
EX:1
const promis=new Promise((resolve,reject)=>{
console.log("Async Execution");
if(false){
resolve();
}
else{
reject();
}
});
promis.then(
()=>{
console.log("Passed");
},
()=>{
console.log("Not Passed No Bikes");
}
)
....................
Ex:2
promis.then(
(val)=>{
console.log(val);
}
).catch(()=>console.log("Failed"))
.finally(()=>{console.log("clean up") });
.............................................
function fetchData() {
return new Promise(function(resolve, reject) {
const data = "Async data";
resolve(data); // Resolve the promise with the data
});
}
setTimeout(() => {
console.log("Vinay");
console.log("namith");
console.log("sashank");
}, 2000);
fetchData()
.then(function(result) {
console.log(result);
})
.catch(function(error) {
console.error(error);
});
....................................................
p.then((val)=>console.log(val));
..................................................
callback Hell:
function fetchData(callback) {
setTimeout(function() {
const data1 = "Data 1";
callback(data1, function(response1) {
// Nested callback
setTimeout(function() {
const data2 = "Data 2";
callback(data2, function(response2) {
// Nested callback
setTimeout(function() {
const data3 = "Data 3";
callback(data3, function(response3) {
// Nested callback
setTimeout(function() {
const result = "Final result";
callback(result);
}, 2000);
});
}, 2000);
});
}, 2000);
});
}, 2000);
}
fetchData(function(data, callback) {
console.log(data);
});
................................................
promise chaining:
function fetchData() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
const data = "Data";
resolve(data);
}, 2000);
});
}
fetchData()
.then(function(data) {
console.log(data);
return fetchData();
})
.then(function(data) {
console.log(data);
return fetchData();
})
.then(function(data) {
console.log(data);
return fetchData();
})
.then(function(data) {
console.log(data);
})
.catch(function(error) {
console.error(error);
});
.........................................
let p=Promise.resolve("done");
p.then((val)=>{
console.log(val);
return "done2"
}).then((val)=>{
console.log(val);
return "done3";
}).then((val)=>console.log(val));
.............................................
fetch('https://jsonplaceholder.typicode.com/todos')
.then(function(response) {
if (!response.ok) {
throw new Error('Network response was not OK');
}
return response.json();
})
.then(function(data) {
console.log(data);
})
.catch(function(error) {
console.error('Error:', error);
});
.................................................