promise
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lib/jquery.js"></script>
<script>
let p = new Promise((resolve, reject) => {
$.ajax({
url: 'data/1.txt',
dataType: 'json',
success(arr) {
resolve(arr);
},
error(err) {
reject(err);
}
});
});
p.then(arr => {
console.log('成功了' + arr);
}, err => {
console.log('失败了');
});
</script>
</head>
<body>
</body>
</html>
promise2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lib/jquery.js"></script>
<script>
let p1 = new Promise((resolve, reject) => {
$.ajax({
url: 'data/1.txt',
dataType: 'json',
success(arr) {
resolve(arr);
},
error(err) {
reject(err);
}
});
});
let p2 = new Promise((resolve, reject) => {
$.ajax({
url: 'data/2.txt',
dataType: 'json',
success(arr) {
resolve(arr);
},
error(err) {
reject(err);
}
});
});
let p3 = new Promise((resolve, reject) => {
$.ajax({
url: 'data/3.txt',
dataType: 'json',
success(arr) {
resolve(arr);
},
error(err) {
reject(err);
}
});
});
Promise.all([p1, p2, p3]).then(arr => {
let [r1, r2, r3] = arr;
console.log(r1);
console.log(r2);
console.log(r3);
}, err => {
console.log("错了");
})
// p.then(arr => {
// console.log('成功了' + arr);
// }, err => {
// console.log('失败了');
// });
</script>
</head>
<body>
</body>
</html>
网友评论