@@ -2487,3 +2487,114 @@ var newStudent ={
24872487}
24882488console.log(newStudent)
24892489====================================================================================================================================================================
2490+ Callback, Promise and Async/await :-
2491+ const data = [ //array of object
2492+ {name: "priya", role: "software developer"},
2493+ {name: "riya", role: "software developer"},
2494+ {name: "supriya", role: "software developer"}
2495+ ];
2496+
2497+ function getData(){
2498+ setTimeout(()=>{
2499+ let output ="";
2500+ data.forEach((item)=>{
2501+ output +=`<li> ${item.name}</li>`
2502+ })
2503+ document.body.innerHTML = output;
2504+ }, 1000) //use 5000 instead of 1000 then we get a name of "dhanupriya".
2505+ }
2506+ function createData(dataInput){
2507+ setTimeout(()=>{data.push(dataInput)}, 2000)
2508+ }
2509+ createData({name: "dhanupriya", role: "software developer"})
2510+ getData()
2511+ =====================================================================
2512+ Callback:
2513+ const data = [ //array of object
2514+ {name: "priya", role: "software developer"},
2515+ {name: "riya", role: "software developer"},
2516+ {name: "supriya", role: "software developer"}
2517+ ];
2518+
2519+ function getData(){
2520+ setTimeout(()=>{
2521+ let output ="";
2522+ data.forEach((item)=>{
2523+ output +=`<li> ${item.name}</li>`
2524+ })
2525+ document.body.innerHTML = output;
2526+ }, 1000)
2527+ }
2528+ function createData(dataInput, callback){
2529+ setTimeout(()=>{
2530+ data.push(dataInput)
2531+ callback(); //getdata function will get call once we push the new dataInput
2532+ },2000)
2533+ }
2534+ createData({name: "dhanupriya", role: "software developer"}, getData) //we are passing getData function as a callback
2535+ ====================================================================
2536+ Promises:
2537+ const data = [ //array of object
2538+ {name: "priya", role: "software developer"},
2539+ {name: "riya", role: "software developer"},
2540+ {name: "supriya", role: "software developer"}
2541+ ];
2542+
2543+ function getData(){
2544+ setTimeout(()=>{
2545+ let output ="";
2546+ data.forEach((item)=>{
2547+ output +=`<li> ${item.name}</li>`
2548+ })
2549+ document.body.innerHTML = output;
2550+ }, 1000) //use 5000 instead of 1000 then we get a name of "dhanupriya".
2551+ }
2552+ function createData(dataInput){
2553+ return new Promise((resolve, reject)=>{
2554+ setTimeout(()=>{
2555+ data.push(dataInput);
2556+ let error = false; //if trure then went to catch block
2557+ if(!error) resolve();
2558+ else reject("Error!!!!");
2559+ },2000)
2560+ })
2561+
2562+ }
2563+ createData({name: "dhanupriya", role: "software developer"})
2564+ .then(getData)
2565+ .catch(err => console.log("Errors !"))
2566+ ====================================================================
2567+ Async/Await:
2568+ const data = [ //array of object
2569+ {name: "priya", role: "software developer"},
2570+ {name: "riya", role: "software developer"},
2571+ {name: "supriya", role: "software developer"}
2572+ ];
2573+
2574+ function getData(){
2575+ setTimeout(()=>{
2576+ let output ="";
2577+ data.forEach((item)=>{
2578+ output +=`<li> ${item.name}</li>`
2579+ })
2580+ document.body.innerHTML = output;
2581+ }, 1000) //use 5000 instead of 1000 then we get a name of "dhanupriya".
2582+ }
2583+ function createData(dataInput){
2584+ return new Promise((resolve, reject)=>{
2585+ setTimeout(()=>{
2586+ data.push(dataInput);
2587+ let error = false; //if trure then went to catch block
2588+ if(!error) resolve();
2589+ else reject("Error!!!!");
2590+ },2000)
2591+ })
2592+ }
2593+
2594+ async function start(){ //using sync and await
2595+ await createData({name: "dhanupriya", role: "software developer"});
2596+ getData();
2597+ }
2598+ start();
2599+ ======================================================================================================================================================================
2600+
0 commit comments