小于 1 分钟
1. Promise 基础使用
function fetchData() {
return new Promise((resolve, reject) => {
// 异步操作
setTimeout(() => {
const data = {
name: 'John',
age: 30
};
if (data) {
resolve(data); // 将Promise对象状态设置为已完成
} else {
reject('Error fetching data'); // 将Promise对象状态设置为已拒绝
}
}, 2000);
});
}
fetchData()
.then(data => {
console.log(data); // 处理已完成状态
})
.catch(error => {
console.error(error); // 处理已拒绝状态
});
小于 1 分钟