- Mastering JavaScript Promises
- Muzzamil Hussain
- 237字
- 2021-07-16 13:46:51
Best practices to handle exceptions in promise
Promise provides an impressive way to handle exceptions. Exception handling in promise is quite simple and easy to implement, and almost all libraries and implementations support a generic way of implementation. Here are some of best practices to deal with exceptions:
Make your exceptions meaningful
To maximize performance and maintainability, throw understandable errors. The best practice is to reject a promise and reject it with an error instance. Make it a habit not to reject an error object or primitive values.
Monitor, anticipate, and handle exception
Keep an eye on the effects of errors on the flow of execution. The best practice for doing this is to anticipate failures in your handlers. The better you are at anticipation, the better will be your control over the flow of execution. Always think whether your rejection handler should be invoked by failures in the resolution handler, or if there should be a different behavior.
Keep it clean
When you are done dealing with exception, start CleanUp as soon as the error occurs. When the chain of promises is processed and a result has been delivered in either rejected or fulfilled state, terminate the chain and clean up the unused thread. This will help in not only optimizing the throughput of code, but also creating manageable outputs.
Mozilla has its own implementation for handling errors in promise, which can be seen at https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise.
- PHP基礎(chǔ)案例教程
- Rust編程從入門到實(shí)戰(zhàn)
- Mastering Natural Language Processing with Python
- The DevOps 2.4 Toolkit
- Visual Basic程序設(shè)計實(shí)驗(yàn)指導(dǎo)(第4版)
- Learn React with TypeScript 3
- Mastering Linux Security and Hardening
- Getting Started with Python and Raspberry Pi
- Serverless Web Applications with React and Firebase
- Nagios Core Administration Cookbook(Second Edition)
- Backbone.js Testing
- 寫給大家看的Midjourney設(shè)計書
- Qt 4開發(fā)實(shí)踐
- 零基礎(chǔ)C#學(xué)習(xí)筆記
- Raspberry Pi Blueprints