-
Notifications
You must be signed in to change notification settings - Fork 783
18f Solution #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 18f
Are you sure you want to change the base?
18f Solution #108
Conversation
console.log(text); | ||
|
||
} catch (error) { | ||
console.log('CORS error. You request was blocked by the backend.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple English mistake here :) "CORS error. You request..." => "CORS error. Your request..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason for using try/catch?
const response = await fetch('https://amazon.com').catch(
error => console.log('CORS error. You request was blocked by the backend.'))
I am answering my own question. I should use try catch and consume the response inside the try block because, if there is an error, I would be left with an uncaught error in the Promise. The way I proposed would only catch the fetch error. Sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you get the response if you use this code?
The data is ready but you haven't collected it to use in your code, so assuming there was no error, how do we get the response?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you get the response if you use this code? The data is ready but you haven't collected in to use in your code, so assuming there was no error, how do we get the response?
It will be stored in the response variable, the you can access it by response.json(), response.text(), response.blob(), etcetera
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you get the response if you use this code? The data is ready but you haven't collected in to use in your code, so assuming there was no error, how do we get the response?
It will be stored in the response variable, the you can access it by response.json(), response.text(), response.blob(), etcetera
That totally makes sense! What situations do we then need to use try{}? If we have many code that may have unexpected errors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an example
async function greetingName() {
const salute = await fetch('https://supersimplebackend.dev/greeting', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
body: JSON.stringify({ name: 'Bob' }),
}).catch(error => console.log(error))
const greeting = await salute.text()
console.log(greeting)
}
greetingName()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an example
async function greetingName() { const salute = await fetch('https://supersimplebackend.dev/greeting', { method: 'POST', headers: { 'Content-Type': 'application/json; charset=UTF-8' }, body: JSON.stringify({ name: 'Bob' }), }).catch(error => console.log(error)) const greeting = await salute.text() console.log(greeting) } greetingName()
Thanks for responding!
No description provided.