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
16 changes: 16 additions & 0 deletions Funny-Bones/fetch-joke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
async function main() {
try {
const response = await fetch("https://official-joke-api.appspot.com/random_joke");
const data = await response.json();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would also handle “failed responses” (HTTP status not OK)

if (!response.ok) {
throw new Error(HTTP ${response.status} ${response.statusText});
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also didnt check for data format

if (typeof data?.setup !== "string" || typeof data?.punchline !== "string") {
throw new Error("Unexpected data format from API");
}

console.log(data.setup);
console.log(data.punchline);
} catch (error) {
console.error("Error: No Jokes for now.", error.message);
}
}
// console.log("1"); //to test the asynchronous behavior
main();
/*console.log("2"); //to test the asynchronous behavior
console.log("3"); //to test the asynchronous behavior
console.log("4"); //to test the asynchronous behavior
console.log("5");*/ //to test the asynchronous behavior
13 changes: 13 additions & 0 deletions Funny-Bones/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "funny-bones",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs"
}