首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从async-await返回rejected?

如何从async-await返回rejected?
EN

Stack Overflow用户
提问于 2019-04-28 08:43:03
回答 1查看 40关注 0票数 2

当fetch请求失败时,我想返回rejected。如何拒绝异步等待?

代码语言:javascript
复制
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)
    })
}
EN

回答 1

Stack Overflow用户

发布于 2019-04-28 08:54:42

According to jQuery docs您应该使用类似于(这主要取自jQuery文档)的符号:

代码语言:javascript
复制
$.getJSON( this.api)
    .done(function( json ) {
    console.log( "JSON Data: " + json.users[ 3 ].name );
  })
  .fail(function( jqxhr, textStatus, error ) {
    var err = textStatus + ", " + error;
    console.log( "Request Failed: " + err );
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55886072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档