Skip to content

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

Open
wants to merge 1 commit into
base: 18f
Choose a base branch
from
Open

18f Solution #108

wants to merge 1 commit into from

Conversation

SuperSimpleDev
Copy link
Owner

No description provided.

console.log(text);

} catch (error) {
console.log('CORS error. You request was blocked by the backend.');

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..."

Copy link

@antaugustol antaugustol Mar 28, 2025

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.

Copy link

@GiftinTech GiftinTech Apr 20, 2025

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?

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

Copy link

@GiftinTech GiftinTech Apr 20, 2025

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?

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()

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants