From a9d8cb623a2e1260e4cf901cc7cea7078c570708 Mon Sep 17 00:00:00 2001 From: Ashok Choudhary Date: Tue, 18 Feb 2025 00:23:06 +0530 Subject: [PATCH] assignment2 of JS --- assignment2/ques1.js | 12 ++++++++++++ assignment2/ques2.js | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 assignment2/ques1.js create mode 100644 assignment2/ques2.js diff --git a/assignment2/ques1.js b/assignment2/ques1.js new file mode 100644 index 0000000..778e1d9 --- /dev/null +++ b/assignment2/ques1.js @@ -0,0 +1,12 @@ +const func = async ()=>{ + console.log("Printing before") + await new Promise((resolve)=>{ + setTimeout(()=>{ + console.log("after waiting for 5 seconds") + resolve() + },5000) + }) + console.log("printing after") +} + +func(); \ No newline at end of file diff --git a/assignment2/ques2.js b/assignment2/ques2.js new file mode 100644 index 0000000..a8fa113 --- /dev/null +++ b/assignment2/ques2.js @@ -0,0 +1,21 @@ +// answer using promise. +const promise1=fetch("https://reqres.in/api/users") + +promise1.then((data)=>{ + console.log(data) +}).catch((error)=>{ + console.error("error occured",error) +}) + +//answer using async await function. +async function fetchData(){ + try{ + const data= await fetch("https://reqres.in/api/users") + console.log(data) + }catch(error){ + console.error("error occured : ",error.message) + } + +} + +fetchData(); \ No newline at end of file