1. 基础熬汤法
// 创建Promise就像煮泡面要有等待计时器
const cookNoodles = new Promise((resolve, reject) => {
setTimeout(() => {
Math.random() > 0.3
? resolve(" 面条Q弹出锅!")
: reject(" 水烧干了!");
}, 2000);
});
// 品尝结果要用.then/.catch
cookNoodles
.then(console.log)
.catch(err => console.log(" 消防员:" + err));
2. 高阶食材搭配术
// Promise链式料理(每一步都传递结果)
function makeRamen() {
return boilWater()
.then