1.0.0 • Published 4 years ago

@essentials/pending v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

A helper function that adds error handling to async/await without having to use try/catch block

Quick Start

import pending from '@essentials/pending'

const someAsyncFunc = async () => {
  const [result, error] = await pending(() => Promise.resolve(true))

  if (error) {
    doSomethingWith(error)
  }

  return result
}

API

async function pending<
  PromiseReturnType extends any = any,
  ErrorType extends any = Error
>(
  callback: () => Promise<PromiseReturnType>
): Promise<[PromiseReturnType | undefined, ErrorType | undefined]>
ArgumentTypeDefaultRequiredDescription
callback() => Promise<PromiseReturnType>undefinedYesA callback or async function that returns the promise you want to gracefully handle errors for.

LICENSE

MIT