xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-03-12 21:51  xgqfrms  阅读(86)  评论(0编辑  收藏  举报