Promise.withResolvers()
假设我们正在构建一个从 API 获取数据的天气应用程序。传统上,我们的代码结构可能如下所示:
function getWeather(city) {
return new Promise((resolve, reject) => {
// 模拟 API 调用
setTimeout(() => {
if (city === "New York") {
resolve("Sunny, 75°F");
} else {
reject("City not found");
}
}, 2000);
});
}
使用 Promise.withResolvers(),我们可以将其重构得更灵活:
function getWeather(city) {
const { promise, resolve, reject } = Promise.withResolvers();
// 模拟 API 调用
setTimeout(() => {
if (city === "New York") {
resolve("Sunny, 75°F");
} else {
reject("City not found");
}
}, 2000);
return promise;
}
// 使用该函数
getWeather("New York")
.then(weather => console.log("Weather:", weather))
.catch(error => console.log("Error:", error));
这种方法使我们能够更好地控制何时以及如何解决或拒绝 Promise,这在复杂的异步场景中特别有用。