定义
Promise是一种异步操作的解决方案,将写法复杂的传统的回调函数和监听事件的异步操作,用同步代码的形式表达出来。
避免了多级异步操作的回调函数嵌套。
目的:为了解决异步任务而诞生,可以将一个异步的流程变为同步的
promise的三种状态
等待状态
resolve 成功的状态 使用then调用
reject 失败的状态 使用catch调用
<script>
/* promise监听 */
var p = new Promise((resolve, reject) => {
resolve("success");
reject("error")
})
/*
resolve then
reject catch
*/
p.then(res=>{
console.log(res)
})
</script>
<script>
/* promise监听 */
var p = new Promise((resolve, reject) => {
// resolve("success");
reject("error")
})
/*
resolve then
reject catch
*/
p.catch(res=>{
console.log(res)
})
</script>