Code Monkey home page Code Monkey logo

Comments (4)

terenced avatar terenced commented on July 23, 2024 2

@kettanaito Thank you!

from until.

sbking avatar sbking commented on July 23, 2024 2

@kettanaito I think this might be incorrect - await should throw on promise rejection

In your example you're just missing an await - add it and console.log is called:

try { await new Promise(() => { throw new Error('Hey') }) } catch (e) { console.log(e) }

from until.

newpost avatar newpost commented on July 23, 2024

@terenced can you tell me why?thank you

from until.

kettanaito avatar kettanaito commented on July 23, 2024

The .catch() block was added to propagate unhandled exceptions to the library.

When there's an exception thrown during a Promise, it will not be caught by a try/catch block around it:

try { new Promise(() => { throw new Error('Hey') }) } catch (e) { console.log(e) }

The console.log will never be called. The thrown exception will be printed as an UnhandledPromiseRejectionWarning.

Flow-wise, that is still an unsuccessful promise execution, so this library should execute the return [error, null] closure for the dependent code to handle it like an error. Adding an explicit .catch() statement to the given promise catches any errors occurring in that promise.

This solution also doesn't interfere with you defining your custom .catch(), as catch doesn't have an accumulative nature. If you've defined your custom .catch() block, it will get executed first, and the library's .catch() will not, unless you throw another exception in your own catch block.

new Promise(() => { throw new Error('Hey') }).catch(e => console.log('Custom', e)).catch(e => console.log('Library', e))

from until.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.