当fetch请求失败时,我想返回rejected。如何拒绝异步等待?
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)
})
}
发布于 2019-04-28 08:54:42
According to jQuery docs您应该使用类似于(这主要取自jQuery文档)的符号:
$.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 );
});
https://stackoverflow.com/questions/55886072
复制相似问题