Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Assignment-8/Problem_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//1. Write a function that can stop execution of a function for the number of milliseconds sent as an argument
// Example:
// const func = async () => {
// console.log(“Printing before”)
// //Call your function here eg. sleep(3000)
// console.log(“Printing after”)
// ()

const sleep=(timeLimit)=>{
return new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve({data:{name:"naziya",lang:"javascript",eng:true}})
},timeLimit)
})
}

console.log("printing before")

const testAsyncFunction = async()=>{
const data= await sleep(3000)
console.log(data)
console.log("Data is fetched")
}
testAsyncFunction()
console.log('Printing after')
22 changes: 22 additions & 0 deletions Assignment-8/Problem_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//2. Using promises - write a function that fetches data from an API endpoint (GET https://reqres.in/api/users ). Log the data into the console once it is received


let promiseResult= fetch('https://reqres.in/api/users')
.then((res)=>{
return res.json()
})
.then((data)=>{
console.log(data)
})


// using async await


const fetchdata= async()=>{
const data1= await fetch('https://reqres.in/api/users')
const sortedData= await data1.json()
console.log(sortedData)
}

fetchdata()
24 changes: 24 additions & 0 deletions Assignment-8/Problem_3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//3. What will be printed to the console when the promise resolves and when it rejects?
const testAsyncFunction = () =>{
return new Promise((resolve, reject) =>{
if (Math.random() > 0.5) {
resolve('Test Resolve');
} else {
reject('Test Reject');
}
}).catch((err) =>{
console.log('Error caught in testAsyncFunction: ', err);
});
};
testAsyncFunction()
.then((res) =>{
console.log('Response in then block: ', res);
})
.catch((err) => console.log('Error in catch block: ', err))

// In case where the Problem is resolved it will log
"Response in then block: Test Resolved"
//// In case where the Problem is rejected it will log
"Error caught in testAsyncFunction: Test Reject"
"Error in catch block: Test Reject"

10 changes: 10 additions & 0 deletions Assignment-8/Problem_4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//What will be printed to the console?
const testAsyncFunction = () =>{
return Promise.reject('Test static reject');
};
testAsyncFunction()
.then((res) =>{
console.log('Response in then block', res);
})
.catch((err) => console.log('Error in catch block', err));

24 changes: 24 additions & 0 deletions Assignment-8/Problem_5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//5. What will be printed to the console?
const testAsyncFunction = () =>{
return new Promise((resolve, reject) =>{
if (Math.random() > 0.5) {
resolve('Test Resolve');
} else {
reject('Test Reject');
}
}).catch((err) =>{
console.log('Error caught in testAsyncFunction', err);
throw new Error('Forced error');
});
};
testAsyncFunction()
.then((res) =>{
console.log('Response in then block: ', res);
})
.catch((err) => console.log('Error in catch block: ', err));

// In Case the promise gets rejected

// 'Error caught in testAsyncFunction:Test Reject'
// Error:Forced error

40 changes: 40 additions & 0 deletions Assignment-8/Problem_6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// //Create a promise that makes a fetch call, but resolves with the data only 2 seconds after the data has been received in the fetch.

// const fetchdelay=()=>{
// return new Promise((resolve,reject)=>{
// setTimeout(()=>{
// return resolve(fetch('https://reqres.in/api/users'))
// },5000)

// }).then((res)=>{
// return res.json()
// }).then((data)=>{
// console.log(data)
// console.log('Data is fetched')
// })

// }
// fetchdelay()
// console.log('data fetching has started')


// with the help of async and await

const fetchdelay=()=>{
return new Promise((resolve,reject)=>{
setTimeout(()=>{
return resolve(fetch('https://reqres.in/api/users'))
},1000)

})
}

let delayfunc=async()=>{
let data= await fetchdelay()
let sorteddata =await data.json()
console.log(sorteddata)
console.log('Data is fetched')
}
delayfunc()
console.log('data fetching has started')