<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
// catch
// 它的可以和then的第二个参数一样,用来指定reject的回调
function cook() {
console.log("开始做饭");
let p =new Promise((resolve,reject) => {
setTimeout(()=>{
console.log("做饭完毕")
reject("鸡蛋炒饭,炒糊了")
},1000)
})
return p;
}
// 开始吃饭
function eat(data) {
console.log("开始吃饭"+data);
let p =new Promise((resolve,reject) => {
setTimeout(()=>{
console.log(data+",吃完了")
resolve("一双筷子一个碗")
},1000)
})
return p;
}
// 开始刷碗
function wash() {
console.log("开始刷碗"+data);
let p =new Promise(resolve => {
setTimeout(()=>{
console.log(data+",刷完了")
resolve("干净的碗筷!")
},2000)
})
return p;
}
cook()
.then(eat)
.then((err)=>{
console.log(err)
})
</script>
</body>
</html>
网友评论