Create a callback.
Write one example explaining how you can write a callback function ?
function one(call){
call()
console.log("After")
}
function call(){
console.log("Before")
}
one(call);
Basic Asynchronus function.
Write callback function to print numbers from 1 to 7, in which 1 should be printed after 1 sec , 2 should be printed after 2 sec, 3 should be printed after 3 sec and so on. Also explain callback hell.
function calltime(){
setTimeout(()=>{
console.log("1");
setTimeout(()=>{
console.log("2");
setTimeout(()=>{
console.log("3");
setTimeout(()=>{
console.log("4");
setTimeout(()=>{
console.log("5");
setTimeout(()=>{
console.log("6")
setTimeout(()=>{
console.log("7")
},7000)
},6000)
},5000)
},4000)
},3000)
},2000)
},1000)
}
calltime()
In asynchronus javascript Callback is a function that is passed as an argument to another function that executes the callback based on the result.
Callback Hell is essentially nested callbacks stacked below one another forming a pyramid structure. Every callback depends/waits for the previous callback, thereby making a pyramid structure that affects the readability and maintainability of the code.
Simple Promise.
Write promise function to print numbers from 1 to 7, in which 1 should be printed after 1 sec , 2 should be printed after 2 sec, 3 should be printed after 3 sec and so on.
let seconds = (timer, num) => {
return new Promise((resolve, reject) => {
if(num){
setTimeout(()=>{
resolve(num());
}, timer)
els {
reject(console.log("No Number"))
}
})
}
seconds(0, ()=>console.log("Start"))
.then(()=>{
seconds(1000, ()=>console.log("1"))
})
.then(()=>{
seconds(2000, ()=>console.log("2"))
})
.then(()=>{
seconds(3000, ()=>console.log("3"))
})
.then(()=>{
seconds(4000, ()=>console.log("4"))
})
.then(()=>{
seconds(5000, ()=>console.log("5"))
})
.then(()=>{
seconds(6000, ()=>console.log("6"))
})
.then(()=>{
seconds(7000, ()=>console.log("7"))
})
Resolve vs Reject.
Create a promise function accepting a argument, if yes is passed to the function then it should go to resolved state and print Promise Resolved, and if nothing is passed then it should go to reject state and catch the error and print Promise Rejected.
let x = function(ans){
return new Promise((resolve,reject) => {
if(ans == 'yes'){
resolve('Promise resolved')
}else{
reject('Promise rejected')
}
})
};
func1("yes")
.then((response) => console.log(response))
.catch((err) => console.error(err));
Callback Example.
Create examples to explain callback function.
function addition(msg,c){
console.log(msg);
}
function c(p,q){
console.log("sum of",p,"&",q,"is",p+q);
}
addition("completed!",c(30,40));
Callback Hell Example.
Create examples to explain callback hell function.
function func(){
func1();
function func1(){
func2();
function func2(){
console.log("Hell..!")
}
}
}
func();
Promise Example.
Create examples to explain promises function.
let compareIt = (num) => {
return new Promise((res, rej) => {
if(num>=10){
res(console.log("Given Number is greater than 10."))
}
else{
rej(console.log("Given Number is smaller than 10."))
}
})
}
compareIt(6)
.then(()=>console.log("Promise Functon."))
.catch(()=>console.log("Reject function."))
Async Await Example.
Create examples to explain async await function.
helloWorld();
console.log("Wait for it...");
function helloWorld(){
setTimeout(()=>{
console.log("Function Hello World!");
}, 2000)
}
Promise.all Example.
Create examples to explain promise.all function.