通过promise将普通函数变成回调函数
clike
let promise = new Promise(function (){})
在这个函数里面可以接收两个参数
resolve 函数 在回调函数中如果调用resolve方法,promise会由pending转换为resolved
reject 函数 在回调函数中如果调用reject方法,promise会由pending转换为reject
clike
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的页面</title>
<script>
let promise = new Promise(function (resolve,reject){
console.log("function invoked")
resolve()
})
console.log("other code1")
// 等待promise对象状态发生改变时才会执行的代码
promise.then(
function(){
// promise转换为resolved状态时会执行的函数
console.log("promise success")
},
function(){
// promise转换为reject状态时会执行的函数
console.log("promise fail")
}
)
console.log("other code2")
</script>
</head>
<body>
</body>
</html>

then里面是可以接收参数的
clike
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的页面</title>
<script>
let promise = new Promise(function (resolve,reject){
console.log("function invoked")
reject("haha")
// resolve()
})
console.log("other code1")
// 等待promise对象状态发生改变时才会执行的代码
promise.then(
function(){
// promise转换为resolved状态时会执行的函数
console.log("promise success")
},
function(data){
// promise转换为reject状态时会执行的函数
console.log("promise fail " + data)
}
)
console.log("other code2")
</script>
</head>
<body>
</body>
</html>
