2

I would like to return rejected, when fetch request is fail. How can i reject async await?

class FetchUrl {
  static async getJson(api) {
    try {
        const response = await fetch(api);
        if (response.ok) {
            const questions = await response.json();
            return questions;
        }
    } catch (error) {
        throw new Error("Request Failed!");
    }
  }
}

 FetchUrl.getJson(this.api).then((resolved) => {
        console.log(resolved)
// this sampe of code is executing even fetch is rejected.. what can i do 
to avoid it?
    }, rejected => {
        console.log(rejected)
    })
}
Rafal9626
  • 39
  • 5

0 Answers0