npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@benzinga/safe-await

v1.0.1

Published

This Project is Made Possible thanks to

Downloads

120

Readme

safe-await

This Project is Made Possible thanks to

Benzinga Logo

ZSoft Logo

Safe Await is a simple way to make sure that our errors are handled.

SafePromise

One of the things I don't like about JavaScript async/await syntax is that it throws on error. This has always caused issues for me since people always forget to try catch these await statements. The other issue I have is the new scope that is created when you try catch which simply makes the code look awful because you have to start thinking about scope of variables.

safeAwait

We don't have to create a safe function we can simply transform the function call to a safe call such as:

const stuff = await safeAwait(fetch('benzinga.com/things/and/stuff'));
if (stuff.err) {
  console.log(stuff.err);
}
else {
  console.log(stuff.ok);
}

As you can see all we are doing here is wrapping fetch with the safeAwait function which does the following:

export type SafePromise<T> = Promise<{ err: Error; ok: undefined } | { err: undefined; ok: T}>

export const safeAwait = async <T>(promise: Promise<T>): SafePromise<T> => {
  try {
    return { ok: await promise };
  } catch (err: any) {
    return { err };
  }
};

One thing to note about both is that safeAwait returns a SafePromise. This is used to indicate that you do not have to try catch the Promise and its an indication that this was checked somewhere upstream. Because the only time you really need to try catch is when calling API functions. After that point Promises are safe unless you are throwing exceptions yourself.

safeAwaitFunction

Another way to solve this issue is to wrap the function to make a safeFunction. So what does safeFunction do? well its very simply it takes a function as an argument and returns a function. Internally it is simply doing the try catch for you and tupalizing the result.

const stuff = await safeFetch('benzinga.com/things/and/stuff');
if (stuff.err) {
  console.log(stuff.err);
} else {
  console.log(stuff.ok);
}

Creating a safe function is easy. Simply call safeAwaitFunction with an async function as a parameter.

const safeFetch = safeAwaitFunction(fetch);

Now we have an implantation of fetch that always returns a SafePromise.

This is the core concept of the safe-await package. The following are simply helper functions and tools that simply use the safe-await library.

Helper Functions

safeRace

Similar to Promise.Race but for safePromises.

safeRace = <T>(promises: SafePromise<T>[]): SafePromise<T>

safeMap

A function that is similar to 'map' for arrays but for safe awaits

example

const quotesExist = (symbol) => {
  safeMap(fetchQuote(symbol), quote => quote.doesExist)
};

safeThen

A function that is similar to Promise.then which allows you to add a function to run after the completion of the promise.

example

const quotesExist = (symbol) => {
  safeThen(fetchQuote(symbol), quote => { ok: quote.doesExist })
};

safeAwaitAll

Similar to Promise.all however it returns a SafePromiseMultiError which allows:

safeAwaitAll = async <U, T extends readonly SafePromise<U>[]>(promise: T & SafePromise<U>[]): SafePromiseMultiError<U[]>