console.log(typeof (Promise)); //function //或 console.dir(Promise);
.访问错误
.网络链接失败、请求超时
.服务器返回 404 错误或非 2xx 或 3xx 状态码
.promise链中任何一个抛出的错误
let promise = fetch(url) promise.then(resolve => { //fulfillment handle }, reject => { //rejection handle })
let promise = fetch(url) promise.then(resolve => { //fulfillment handle })
let promise = fetch(url) promise.then(null, reject => { //rejection handle })
分类 | 说明 |
---|---|
运行时错误 | 程序执行过程中发生的错误,例如除以零、数组越界、空指针引用等 |
自定义异常 | 自定义异常来表示特定的错误条件或业务规则违反 |
系统异常 | 操作系统或编程语言环境可能会抛出一些系统级别的异常,比如文件未找到、权限不足等 |
库或框架异常 | 使用第三方库或框架时,如果调用的方法内部出现错误,这些库或框架可能会抛出异常 |
类型异常 | 在强类型语言中,类型不匹配或其他类型相关的错误也可能导致异常 |
逻辑错误 | 逻辑错误通常不会立即导致程序崩溃;某些情况下,可以选择通过抛出异常的方式来中断程序流程,便于调试和处理 |
let promise = fetch(url) promise.catch(reject => { //rejection handle })
let promise = fetch(url) promise.then(resolve => { //fulfillment handle }) promise.catch(reject => { //rejection handle })
let promise = fetch(url) promise.finally(log => { console.log('log', log); })
let promises = Promise .all([f0(), f1(), f2()]) .then(e => { console.log('all ok') console.dir(e) }) .catch(err => { console.log('err', err) }) .finally(() => { console.log('all done') }) function f0() { return fetch('/utils/data/cart.json') .then(res => res.json()) } function f1() { return fetch('/utils/data/form.json') .then(res => res.json()) } function f2() { return fetch('/utils/data/coffee.json') .then(res => res.json()) }
let promise = fetch(url) promise.then(res => { // fulfillment handle }).catch(err => { // rejection handle }).finally(log => { // finally handle })
pa() .then(res => { // 拿到的是pa的结果 console.log(res); return pb() }) .then(res => { //拿到的是pb的结果 console.log(res); return pc() }) .then(res => { //拿到的是pc的结果 console.log(res); })
let promise = new Promise((resolve, reject) => { resolve('fulfilled') reject('rejected') }) promise.then(res => { console.log(res); }, err => { console.log(err); }) promise.catch(err => { console.log(err); }) promise.finally(log => { console.log('ending for log'); })
let promise = new Promise((resolve, reject) => { resolve('fulfilled') reject('rejected') }) .then(res => { console.log(res); }, err => { console.log(err); }) .catch(err => { console.log(err); }) .finally(log => { console.log('ending for log'); })
待处理 - 调用Promise时,初始状态为 pending,表示还没有开始
let promise = new Promise((resolve, reject) => { }) console.log(promise);
执行成功时,调用resolve,将Promise的状态改为fulfilled
let promise = new Promise((resolve, reject) => { resolve('fulfilled') }) console.log(promise); promise.then(res => { console.log(res); })
Promise 执行过程中遇到错误或者无法完成预期操作时,Promise的状态改为rejected
let promise = new Promise((resolve, reject) => { reject('rejected') }) console.log(promise); promise.catch(err => { console.log(err); })
let flag = false let promise = new Promise((resolve, reject) => { flag ? resolve('data: fulfilled') : reject('data rejected'); }) .then(res => { console.log(res); }, err => { console.log('then err', err); //拒绝后,如果这里处理过了,catch()就不再处理 }) .catch(err => { console.log('catch err', err); //如果then()中没有处理异常,这里就必须处理 }) .finally(log => { console.log('ending for log'); })