官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 叙永县| 永宁县| 宁武县| 乌拉特中旗| 壶关县| 沙雅县| 淄博市| 灵台县| 莱阳市| 尼木县| 阿拉善盟| 屯门区| 靖江市| 大安市| 太康县| 綦江县| 石渠县| 山东| 长治县| 沈阳市| 五台县| 西丰县| 景宁| 稷山县| 贺兰县| 六盘水市| 眉山市| 五莲县| 榆社县| 元氏县| 蒲城县| 甘洛县| 睢宁县| 扎赉特旗| 日土县| 安阳市| 晋州市| 中牟县| 双辽市| 彭山县| 克什克腾旗|