loadAsync1()
.then(function(data1) {
return loadAsync2(data1)
})
.then(function(data2){
return loadAsync3(data2)
})
.then(okFn, failFn)
******************* *****************
loadAsync1()
.then(function(data1) {
loadAsync2(data1)
})
.then(function(data2){
loadAsync3(data2)
})
.then(res=>console.log(res))
复制代码
不等同状况,此时catch捕获并非ajaxLoad1错误 而是ajaxLoad2的错误。**看场景有时要结合起来使用**
```js
ajaxLoad1()
.then(res=>{ return ajaxLoad2() })
.catch(err=> console.log(err))
----------------------
// 结合使用
ajaxLoad1()
.then(res=>{ return ajaxLoad2() }, err=>console.log(err))
.catch(err=> console.log(err))
```
复制代码
若是then或catch接收的不是函数,那么就会发生穿透行为,因此在应用过程当中,应该保证then接收到的参数始终是一个函数ajax
new Promise(resolve=>resolve(8))
.then(1)
.catch(null)
.then(Promise.resolve(9))
.then(res=> console.log(res))
// 8
复制代码
// 0 未初始化未调用open
// 1.启动 调用open 未调用 send
// 2. 发送 已调用send() 可是未响应
// 3. 接收 已经接收部分响应数据
// 4.完成 完成所有数据响应
const ajax = function (params) {
if (!params.url) return
const promise = new Promise((resolve, reject) => {
const handler = function () {
if (this.readyState !== 4) return
if (this.status == 200) {
try {
let resonse = JSON.parse(this.responseText)
resolve(resonse)
} catch (error) {
reject(error)
}
} else {
reject(new Error(this.statusText))
}
}
const xhr = new XMLHttpRequest()
if (params.method.toLowerCase() == 'get') {
xhr.open('get', url + '?' + formatParams(params.data));
xhr.send()
} else {
xhr.open('post', url);
xhr.send(JSON.stringify(params.data));
}
xhr.onreadystatechange = handler
xhr.responseType = 'json'
xhr.setRequestHeader('Accept', 'application/json');
})
return promise
function formatParams(obj) {
if (!data) return
var arr = []
for (let i in obj) {
arr.push(`${encodeURIComponent(i)}=${encodeURIComponent(obj[i])}`)
}
return arr.join('&')
}
}
复制代码
- promise.then(onFulfilled, onRejected)
在onFulfilled中发生异常的话,在onRejected中是捕获不到这个异常的。
- promise.then(onFulfilled).catch(onRejected)
.then中产生的异常能在.catch中捕获
复制代码
```JS
/* 例4.1 */
function taskA() {
console.log(x);
console.log("Task A");
}
function taskB() {
console.log("Task B");
}
function onRejected(error) {
console.log("Catch Error: A or B", error);
}
function finalTask() {
console.log("Final Task");
}
var promise = Promise.resolve();
promise
.then(taskA) // 抛出错误,不继续Task A”
.then(taskB) // .then没有捕获A抛出的错,不打印 “Task B”
.catch(onRejected) // 捕获了A的错,打印错误信息
.then(finalTask); // 错误已经被捕获,执行resolve
-------output-------
Catch Error: A or B,ReferenceError: x is not defined
Final Task
```
复制代码
```JS
//方法1:对同一个promise对象同时调用 then 方法
var p1 = new Promise(function (resolve) {
resolve(100);
});
p1.then(function (value) {
return value * 2;
});
p1.then(function (value) {
return value * 2;
});
p1.then(function (value) {
console.log("finally: " + value);
});
-------output-------
finally: 100
------------------------------------------------------
//方法2:对 then 进行 promise chain 方式进行调用
var p2 = new Promise(function (resolve) {
resolve(100);
});
p2.then(function (value) {
return value * 2;
}).then(function (value) {
return value * 2;
}).then(function (value) {
console.log("finally: " + value);
});
-------output-------
finally: 400
```
复制代码
```JS
// Errors thrown inside asynchronous functions will act like uncaught errors
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
throw 'Uncaught Exception!';
}, 1000);
});
promise.catch(function(e) {
console.log(e); //This is never called
});
```复制代码