// promise
let promise = new Prommise((resolve, reject) => {
setTimeout(() => {
if (true) {
resolve({ name: "狗" });
} else {
reject("失败了");
}
});
}, 2000);
//promise.then(onCompleted, onRejected);
//onCompleted必需。承诺成功完成时要运行的履行处理程序函数。
//onRejected可选。承诺被拒绝时要运行的错误处理程序函数
promise.then(result => console.log(result));
//.catch的作用是捕捉前一个函数的错误
promise.catch(error => console.log(error));
promise.then(result => console.log(result)) //{ name: "狗" }
.catch(error => console.log(error)) //失败了
//async 返回的是一个promise对象
const fn = async() =>{}
async function fn(){}
//(“util”).promisify(包装方法成为promise对象)