2022-04-05
1971
#vanilla javascript
Vijit Ail
101953
Apr 5, 2022 â‹… 7 min read

How to write a declarative JavaScript promise wrapper

Vijit Ail Software Engineer at toothsi. I work with React and NodeJS to build customer-centric products. Reach out to me on LinkedIn or Instagram.

Recent posts:

how to use the ternary operator in javascript

How to use the ternary operator in JavaScript

Add to your JavaScript knowledge of shortcuts by mastering the ternary operator, so you can write cleaner code that your fellow developers will love.

Chizaram Ken
Feb 21, 2025 â‹… 7 min read
Using tsup To Bundle Your TypeScript Package

Using tsup to bundle your TypeScript package

Learn how to efficiently bundle your TypeScript package with tsup. This guide covers setup, custom output extensions, and best practices for optimized, production-ready builds.

Muhammad Ali
Feb 20, 2025 â‹… 7 min read
how to use react higher order components

How to use React higher-order components

Learn the fundamentals of React’s high-order components and play with some code samples to help you understand how it works.

Hussain Arif
Feb 20, 2025 â‹… 10 min read
dependency inversion principle

Understanding the dependency inversion principle (DIP)

Learn about the dependency inversion principle (DIP), its importance, and how to implement it across multiple programming languages.

Samuel Olusola
Feb 20, 2025 â‹… 8 min read
View all posts

2 Replies to "How to write a declarative JavaScript promise wrapper"

  1. Another way to handle the try catch if/else is to use the await keyword but use .catch on that promise if you don’t want the outer try catch involved. You can also handle it in the .catch and also re throw it if you would want to halt the execution flow.

    try {
    // business logic includes exception so nds particular handling
    const data = await something()
    .catch(th => {
    // process exception
    // rethrow if some condition
    });
    // only caught by outer bc it’s a zero sum expectation for example
    const more = await someone();
    } catch (th) {
    // handle th
    }

  2. The approach is similar to monad-transformer TaskEither

    https://gcanti.github.io/fp-ts/modules/TaskEither.ts.html

    Actually there is a significant difference between Either.Left and Exception.

    The first one should be used for “recovable” errors, the second one — for unrecoverable.

    So it means we don’t need to avoid throwing an exception in all cases, replacing them with error-result tuple. And the promiser can help with that.

    Nevertheless, the movement to functional programming is great.

Leave a Reply