Open zhuanghaixin opened 4 years ago
console.log(1);
setTimeout(function() {
console.log(2);
}, 0);
new Promise(function(resolve) {
console.log(3);
resolve(Date.now());
}).then(function() {
console.log(4);
setTimeout(()=>{console.log(8)},0)
});
console.log(5);
setTimeout(function() {
new Promise(function(resolve) {
console.log(6);
resolve(Date.now());
}).then(function() {
console.log(7);
});
}, 0);
1,3,5,4,2,6,7,8
const p1=new Promise((resolve,reject)=>{
resolve(1)
})
const p2=new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(2)
},1000)
})
const p3=new Promise((resolve,reject)=>{
setTimeout(() => {
reject(3)
},1000)
})
console.log(p1)
console.log(p2)
console.log(p3)
setTimeout(()=>{
console.log(p2)
},2000)
setTimeout(()=>{
console.log(p3);
},2000)
p1.then(res=>{
console.log(res)
})
p2.then(res=>{
console.log(res)
})
// p3.then(()=>{
// },(res)=>{
// console.log(res)
// }) //等价于 .catch
p3.catch(res=>{
console.log(res)
})
// callback hell
ajax('static/a.json', res => {
console.log(res)
ajax('static/b.json', res => {
console.log(res)
ajax('static/c.json', res => {
console.log(res)
})
})
})
封装的ajax函数
// Ajax的原理
function ajax(url, callback) {
// 1、创建XMLHttpRequest对象
var xmlhttp
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest()
} else { // 兼容早期浏览器
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
}
// 2、发送请求
xmlhttp.open('GET', url, true)
xmlhttp.send()
// 3、服务端响应
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var obj = JSON.parse(xmlhttp.responseText)
// console.log(obj)
callback(obj)
}
}
}
new Promise((resolve, reject) => {
ajax('static/a.json',res=>{
console.log(res)
resolve()
})
}).then(()=>{
console.log('a成功')
return new Promise((resolve, reject)=>{
ajax('static/b.json',res=>{
console.log(res)
resolve()
})
})
}).then(()=>{
console.log('b成功')
return new Promise((resolve, reject)=>{
ajax('static/c.json',res=>{
console.log(res)
resolve()
})
})
}).then(()=>{
console.log('c成功')
})
function getPromise(url) {
return new Promise((resolve, reject) => {
ajax(url, res => {
resolve(res)
})
})
}
getPromise('static/a.json')
.then(res => {
console.log(res)
return getPromise('static/b.json')
})
.then(res => {
console.log(res)
return getPromise('static/c.json')
}
)
.then(res => {
console.log(res)
})
封装的ajax函数
// Ajax的原理
function ajax(url, successCallback, errorCallback) {
// 1、创建XMLHttpRequest对象
var xmlhttp
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest()
} else { // 兼容早期浏览器
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
}
// 2、发送请求
xmlhttp.open('GET', url, true)
xmlhttp.send()
// 3、服务端响应
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var obj = JSON.parse(xmlhttp.responseText)
// console.log(obj)
successCallback && successCallback(obj) //函数和有就调用,函数没有传递,就不调用
}else if (xmlhttp.readyState === 4 && xmlhttp.status === 404) {
errorCallback && errorCallback(xmlhttp.responseText) }
}
}
function getPromise(url) {
return new Promise((resolve, reject) => {
ajax(url, res => {
resolve(res)
},err=>{
reject(err)
})
})
}
getPromise('static/aa.json')
.then(res => {
console.log(res)
},err=>{
console.log(err)
// return getPromise('static/b.json')
})
.then(res => {
console.log(res)
// return getPromise('static/c.json')
}
)
.then(res => {
console.log('最后一次then')
console.log(res)
})
function getPromise(url) {
return new Promise((resolve, reject) => {
ajax(url, res => {
resolve(res)
},err=>{
reject(err)
})
})
}
getPromise('static/aa.json')
.then(res => {
console.log(res)
},err=>{
console.log(err)
// return getPromise('static/b.json')
})
.then(res => {
console.log(res)
return getPromise('static/c.json')
}
)
.then(res => {
console.log('最后一次then')
console.log(res)
})
function getPromise(url) {
return new Promise((resolve, reject) => {
ajax(url, res => {
resolve(res)
},err=>{
reject(err)
})
})
}
getPromise('static/aa.json')
.then(res => {
console.log(res)
})
.then(res => {
console.log(res)
return getPromise('static/c.json')
}
)
.then(res => {
console.log(res)
})
.catch(err => {
console.log('文件找不到')
console.log(err)
})
let p1 = Promise.resolve('success')
// console.log(p1)
p1.then(res => {
console.log(res)
})
``js let p2 = Promise.reject('fail') console.log(p2) p2.catch(err => { console.log(err) })
```js
function foo(flag) {
if (flag) {
return new Promise(resolve => {
// 异步操作
resolve('success')
})
} else {
// return 'fail'
return Promise.reject('fail')
}
}
foo(false).then(res => {
console.log(res)
}, err => {
console.log(err)
})
let p1 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(1)
resolve('1成功')
}, 2000)
})
let p2 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(2)
resolve('2成功')
}, 1000)
})
let p3 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(3)
resolve('3成功')
}, 3000)
})
Promise.all([p1, p2, p3]).then(res => {
console.log(res)
}, err => {
console.log(err)
})
let p1 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(1)
resolve('1成功')
}, 2000)
})
let p2 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(2)
// resolve('2成功')
reject('2失败')
}, 1000)
})
let p3 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(3)
resolve('3成功')
}, 3000)
})
Promise.all([p1, p2, p3]).then(res => {
console.log('成功了')
console.log(res)
}, err => {
console.log('失败了')
console.log(err)
})
const imgArr = ['1.jpg', '2.jpg', '3.jpg']
let promiseArr = []
imgArr.forEach(item => {
promiseArr.push(new Promise((resolve, reject) => {
// 图片上传的操作
resolve()
}))
})
Promise.all(promiseArr).then(res => {
// 插入数据库的操作
console.log('图片全部上传完成')
})
有一个成功,就执行成功的回调函数
let p1 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(1)
resolve('1成功')
}, 1000)
})
let p2 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(2)
// resolve('2成功')
reject('2失败')
}, 2000)
})
let p3 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(3)
resolve('3成功')
}, 3000)
})
Promise.race([p1, p2, p3]).then(res => {
console.log(res)
}, err => {
console.log(err)
})
function getImg() {
return new Promise((resolve, reject) => {
let img = new Image()
img.onload = function () {
resolve(img)
}
// img.src = 'http://www.xxx.com/xx.jpg'
img.src = 'https://www.imooc.com/static/img/index/logo.png'
})
}
function timeout() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('图片请求超时')
}, 2000)
})
}
Promise.race([getImg(), timeout()]).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
Promise
为什么需要Promise?
}).then(()=>{ // 2.then(x,y)可以接受两个参数,第一个参数是必须的,第二个参数可以省略 //2.1 第一个异步操作执行成功,会进到第一个参数的函数里面 console.log('成功') },()=>{ //2.2 第一个个异步操作执行成功,会进到第二个参数的函数里面 console.log('失败') })
注意
第一个例子Promise代码还会马上执行
第二个例子 微任务 ,then()里面的
第三个例子
宏任务与微任务