js Promise In Deep All In One
js Promise In Deep All In One
Promise Lifecycle
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
// fetch() promises to complete at some point in the future
const promise = fetch("./books.json");
promise.then(res => {
// success
}, err => {
// error
});
promise.then(res => {
// success
}).catch(err => {
// error
});
// loading = true;
promise.then(res => {
// success
}).catch(err => {
// error
}).finally(() => {
// loading = false;
});
unsettled
A pending promise is considered unsettled.
- pending state
The promise is in the pending state as soon as the fetch() function returns it.
settled
Once the promise completes, the promise is considered settled and enters one of two possible states.
- Fulfilled: The promise has been completed successfully.
- Rejected: The promise didn’t complete successfully due to either an error or some other cause.
then & thenable
The then() method is present on all promises and takes two arguments.
Any object that implements the then() method in this way is called a thenable
.
All promises are thenables
, but not all thenables
are promises.
fulfillment handler
rejection handler
const promise = fetch("books.json");
// both of fulfillment handler and rejection handler
promise.then(response => {
// fulfillment
console.log(response.status);
}, reason => {
// rejection
console.error(reason.message);
});
// only fulfillment handler
promise.then(response => {
// fulfillment
console.log(response.statusText);
});
// only rejection handler
promise.then(null, reason => {
// rejection
console.error(reason.message);
});
catch error
Just know that if you don’t attach a rejection handler to a promise that is rejected, then the JavaScript runtime will output a message to the console, or throw
an error, or both (depending on the runtime).
// shortcut
promise.catch(reason => {
// rejection
console.error(reason.message);
});
// is the same as
promise.then(null, reason => {
// rejection
console.error(reason.message);
});
settlement handler
finally
The callback passed to finally is called regardless of success or failure.
callbacks do not receive any arguments because it isn’t clear whether the promise was fulfilled or rejected.
Because the settlement handler is called both on fulfillment and. rejection, it is similar (but not the same) to passing the handler for both fulfillment and rejection using then().
const promise = fetch("books.json");
// shortcut
promise.finally(() => {
// no way to know if fulfilled or rejected
console.log("Settled");
});
// is similar to
const callback = () => {
console.log("Settled");
};
promise.then(callback, callback);
The settlement handlers added with finally() do not prevent rejections from outputting an error to the console or throwing an error.
You must still add a rejection handler to prevent the error from being thrown in that case. ⚠️
task & Microtasks
refs
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15948021.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-02-28 Apple TV (6th) All In One
2021-02-28 macOS show wifi password All In One
2021-02-28 Apple Store 美国账号注册 All In One
2021-02-28 Apple TV All In One
2021-02-28 Apple 企业采购优惠政策 All In One
2021-02-28 4K 视频 All In One
2020-02-28 SVG 场馆图