Promise 是 JavaScript 处理异步的核心机制,具有 pending、fulfilled、rejected 三种不可逆状态;通过 new Promise 创建,用 .then() 处理成功、.catch() 统一捕获错误,支持链式调用与值透传,并行用 Promise.all() 或容错用 Promise.allSettled()。
Promise 是 JavaScript 中处理异步操作的核心机制,它让原本嵌套回调(callback hell)的代码变得线性、可读、易维护。用好 Promise 的关键不是记住语法,而是理解它的状态流转和链式逻辑。
Promise 有 pending(进行中)、fulfilled(成功)、rejected(失败)三种状态,一旦改变就不可逆。创建 Promise 需传入一个执行器函数(executor),它接收 resolve 和 reject 两个函数作为参数:
示例:封装一个延迟 1 秒后返回数据的异步操作
const fetchData = () => new Promise((resolve, reject) => {
setTimeout(() => {
const success = Math.random() > 0.3;
if (success) {
resolve({ data: 'Hello World' });
} else {
reject(new Error('Network failed'));
}
}, 1000);
});
.then() 接收两个可选回调:第一个处理 fulfilled 状态,第二个处理 rejected(不推荐这样写);更推荐统一用 .catch() 捕获错误,因为:
catch()正确写法:
fetchData()
.then(res => console.log(res.data))
.catch(err => console.error('出错了:', err.message));
每个 .then() 返回一个新的 Promise。如果返回的是普通值(如字符串、对象),会被自动包装成 resolved Promise;如果返回另一个 Promise,则等待它完成后再继续链式执行。这使得“串行异步”非常自然:
示例:
getUserID() .then(id => fetchUserInfo(id)) // 返回 Promise .then(user => render(user)) .catch(err => showError(err));
多个独立请求需要同时发起时,别写多个 .then() 嵌套,用 Promise.all() 并发执行:
常见场景:同时拉取用户信息和最新通知
Promise.allSettled([
fetchUser(),
fetchNotifications()
]).then(results => {
const [userRes, notifyRes] = results;
if (userRes.status === 'fulfilled') showUser(userRes.value);
if (notifyRes.status === 'fulfilled') showNotify(notifyRes.value);
});