Promise.then(函数)的用法

// 1. 创建一个 Promise(模拟异步)
let myPromise = new Promise((resolve) => {
  setTimeout(() => {
    resolve("我是异步成功的数据!"); // 成功时调用
  }, 1000);
});

// 2. 使用 .then() 接收结果
// 方案一(系统函数)
myPromise.then(console.log);

// 方案二(箭头函数)
myPromise.then((result) => {
    console.log(result);})

// 方案三(普通函数)
// 定义一个普通函数
function printData(data) {
  console.log(data);
}
myPromise.then(printData);

运行结果如下所示:

image

posted @ 2026-03-29 11:40  chenlight  阅读(3)  评论(0)    收藏  举报