promise then catch finally All in One
promise then catch finally All in One
fetch('https://cdn.xgqfrms.xyz/json/fast/1.json')
.then((res) => {
if(res?.status === 200) {
console.log('res', res);
return res.json();
} else {
throw new Error(`http error, http status code = ${res?.status}`);
}
})
.then((json) => {
console.log('json', json, JSON.stringify(json, null, 4));
return json;
})
.catch((err) => {
console.log('error', err);
return err;
})
.finally((ok, err) => {
console.log('finally', ok, err);
// this.loading = false;
});
fetch('https://cdn.xgqfrms.xyz/json/fast/x.json')
.then((res) => {
if(res?.status === 200) {
console.log('res', res);
return res.json();
} else {
throw new Error(`http error, http status code = ${res?.status}`);
}
})
.then((json) => {
console.log('json', json, JSON.stringify(json, null, 4));
return json;
})
.catch((err) => {
console.log('error', err);
return err;
})
.finally(() => {
// finally 不会收到任何 Promise 的信息,只会保证在最后一定执行
console.log('finally clean action')
// this.loading = false;
});
// .finally((ok, err) => {
// console.log('finally', ok, err);
// // this.loading = false;
// });
refs
https://gist.github.com/xgqfrms/8b032c59e28e7f9807e74ee271839394
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14526588.html
未经授权禁止转载,违者必究!