为什么在JS中使用异常来拒绝承诺? [英] Why are exceptions used for rejecting promises in JS?

查看:120
本文介绍了为什么在JS中使用异常来拒绝承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是 http://promises-aplus.github。 IO /承诺规格/

当使用然后()时,您可以返回一个承诺,并在您需要时拒绝承诺,或者你可以抛出异常拒绝承诺。

When using then(), you can either return a promise, and reject the promise when you desire, or you can throw an exception to reject a promise.

为什么api不是以这种方式设计的,对于然后函数,它传递了一个解决方案和拒绝函数像原始的承诺构造函数?

Why wasn't the api designed in such a fashion where for the then function, it is passed a resolve and reject function like the original promise constructor?

许多语言中的例外情况很严重(我也假设在javascript中)所以看起来很奇怪他们将它们用作流量控制的选择。创建一个全新的promise对象并返回它,只是为了拒绝它,增加了代码膨胀IMO。如果抛出异常(例如语法错误,或者在未定义的对象上调用函数等),调试也会变得更难。

Exceptions are heavy in many languages (and I assume in javascript too) so it seems odd they are using them as a choice for flow control. Creating a whole new promise object and returning it, just to reject it, adds to code bloat IMO. Debugging becomes harder too in case of an exception is thrown (such as syntax errors, or if function is being called on a undefined object, etc.)

推荐答案


为什么api不是以这种方式设计的,对于then函数,它传递一个像原始promise构造函数一样的解析和拒绝函数?

Why wasn't the api designed in such a fashion where for the then function, it is passed a resolve and reject function like the original promise constructor?

实际上,该规范中的API在各种实现中成为共识。但是,可能导致这一点的一些要点是:

Actually, the API in that spec emerged as a consensus amongst various implementations. However, some points that might have led to this are:


  • 然后是一个相当实用的方法。它的回调只应该接收一个数据参数,即promise的结果值。

  • 传递额外的解析 / 拒绝回调函数不适用于多个参数甚至可变函数。

  • 然后是通常用作普通映射函数。你只需返回新值,不需要解决

  • 当你真的想在你的回调中做一些异步,你可以使用 resolve / 拒绝,你最好使用无论如何都是一个承诺 - 你可以随时返回。

  • then is a rather functional method. It's callback only should receive one data argument, the result value of the promise.
  • Passing additional resolve/reject functions to the callback does not work well with multiple arguments or even variadic functions.
  • then is usually used as a plain mapping function. You simply return the new value, no resolve is needed.
  • When you really want to do something asynchronous in your callback where you could make use of resolve/reject, you better should use a promise anyway - which you simply can return then.

我曾经用可选的 resolve <实现了一个Promise lib / code> / 拒绝参数,但使用起来很繁琐 - 因为#4我很少需要它们。使用它们容易出错,你可能很容易忘记某些事情(比如处理错误或进度事件) - 就像手动构建和返回从promise回调中解决的延迟一样的人而不是调用然后

I once implemented a Promise lib with optional resolve/reject arguments, but it was tedious to use - and I seldom needed them because of #4. Using them was error-prone, you could easily forget something (like handling errors, or progress events) - just like the people who are manually constructing and returning deferreds that are resolved from promise callbacks, instead of calling then.


异常很重,所以看起来很奇怪他们将它们用作流量控制的选择。

Exceptions are heavy so it seems odd they are using them as a choice for flow control.

它们实际上并不是用于控制流(如分支,循环等),而是用于异常处理拒绝是特殊的。大多数Promise开发人员都希望将它们作为同步(阻塞)代码的替代方案来实现 - 其中IO总是抛出异常,因此他们对此进行了调整。拒绝仍然被解释为异步等同于 try ... catch ,尽管它们的monadic性质可以用于更强大的方式和更高级别的应用程序。

They're not really meant to be used for control flow (like branching, loops etc) but for exception handling: rejections are exceptional. Most Promise developers wanted to implement them as an alternative for synchronous (blocking) code - where IO was always throwing exceptions, so they adapted this. Rejections are still explained as the asynchronous equivalent to try … catch, though their monadic nature could be utilized in mightier ways and higher-level applications.


创建一个全新的promise对象并返回它,只是为了拒绝它,增加了代码膨胀IMO。

Creating a whole new promise object and returning it, just to reject it, adds to code bloat IMO.

返回新的RejectedPromise(...)返回拒绝(...)和抛出错误(...)


调试变得更难以防万一抛出异常(例如语法错误,或者在未定义的对象上调用函数等)。

Debugging becomes harder too in case of an exception is thrown (such as syntax errors, or if function is being called on a undefined object, etc.)

大多数Promise开发人员似乎将此视为优势实际上 - (意外)异常即使在异步代码中也会被自动捕获,因此可以处理它们而不是炸毁程序(不被注意)。另请参阅异常处理,抛出错误,承诺中的内容'LOUD'错误的可接受承诺模式?

Most Promise developers seem to see this as an advantage actually - (unexpected) exceptions even in asynchronous code will be caught automatically, so they can be handled instead of blowing up the program (unnoticed). See also exception handling, thrown errors, within promises and acceptable promise pattern for 'LOUD' errors?.

这篇关于为什么在JS中使用异常来拒绝承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆