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
26 changes: 12 additions & 14 deletions exercise001.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const promise = new Promise((resolve, reject) => {
// Generate random number between 0 and 9
const randomInt = Math.floor(Math.random() * 10);

if (randomInt % 2 === 0) {
// Success
setTimeout(() => resolve('done'), 2000);
} else {
// Failure
setTimeout(() => reject('error'), 2000);
}
});

// Your solution(s) to exercise001 here!
const getPromiseResponse = async (resolve, reject) => {
const randomInt = await Math.floor(Math.random() * 10);
if (randomInt % 2 === 0) {
console.log(resolve);
} else {
console.log(reject);
}
};
getPromiseResponse(
`Yay! Promise resolved with response:done`,
`Boo. Promise rejected with response:error`
);
15 changes: 9 additions & 6 deletions exercise002.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import fetch from "node-fetch";

const jsonTypicode = "https://jsonplaceholder.typicode.com/todos/1";

const fetchData = (apiEndPoint) => {
fetch(apiEndPoint)
.then((response) => response.json())
.then((json) => console.log(json))
.catch((error) => console.log(error));
};
async function fetchData(apiEndPoint) {
try {
const response = await fetch(apiEndPoint);
const json = await response.json();
console.log(json);
} catch {
console.log(error);
}
}

fetchData(jsonTypicode);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"doc": "docs"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node docs/exercise001.js"
},
"repository": {
"type": "git",
Expand Down