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:

Building a Full-Featured Laravel Admin Dashboard with Filament

Building a full-featured Laravel admin dashboard with Filament

Build scalable admin dashboards with Filament and Laravel using Form Builder, Notifications, and Actions for clean, interactive panels.

Kayode Adeniyi
Dec 20, 2024 â‹… 5 min read
Working With URLs In JavaScript

Working with URLs in JavaScript

Break down the parts of a URL and explore APIs for working with them in JavaScript, parsing them, building query strings, checking their validity, etc.

Joe Attardi
Dec 19, 2024 â‹… 6 min read
Lazy Loading Vs. Eager Loading

Lazy loading vs. Eager loading

In this guide, explore lazy loading and error loading as two techniques for fetching data in React apps.

Njong Emy
Dec 18, 2024 â‹… 5 min read
Deno logo over an orange background

How to migrate your Node.js app to Deno 2.0

Deno is a popular JavaScript runtime, and it recently launched version 2.0 with several new features, bug fixes, and improvements […]

Yashodhan Joshi
Dec 17, 2024 â‹… 7 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