Skip to content

Async function errors should not be raised in the main thread #39

@king-of-poppk

Description

@king-of-poppk

Using coro 1.0.3, promises 1.2.0.1, and future 1.28.0, I get the following unexpected behavior. Running the following code, the error is raised in the main thread (and not handled by the onRejected error handler, the execution does not reach the part of the code that defines the handlers):

f <- async(\() {
  stop(":(")
})

p <- f()

p %>% then(
  onFullfilled = \(value) { print(value) },
  onRejected = \(error) { print(error$message) }
)

The same goes if I have some await statement after stop(.). Of course, if I await a promise before the stop(.) all goes well.

It seems to me that whatever compiles this down to promises makes the first block of synchronous instructions execute immediately, which is also how things are implemented in other languages/execution environments (NodeJS for instance).

However, everything that happens inside an asynchronous function should be handled by declared handlers, and eventually, if some errors are not handled those should raise some warnings about unhandled rejections.

I am not sure if that should also be the case for promises as defined in the promises package, and maybe that is where the error originates from. In JavaScript for example, they have made the decision to also nicely handle direct errors raised within a Promise executor function (although one could argue the only proper way to raise an error for a promise in JavaScript is to call the reject handler).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions