Git Product home page Git Product logo

try's Issues

Alternative API

I'm glad someone released a library like this, I've been using this pattern for quite some time now, it rocks!

This issue is only a suggestion for what could be, perhaps, an alternative export with a different return signature

import { $try } from '@bdsqqq/try'

const promise = () => 
  new Promise((res) => 
    setTimeout(() => res('aha!'), 1000)
  )

const promiseThatRejects = () => 
  new Promise((_, rej) => 
    setTimeout(() => rej(new Error('oh no')), 1000)
  )

const result = await $try(promise())
if (!result.error) console.log(result.data)

const otherResult = await $try(promiseThatRejects())
if (!otherResult.error) console.log(otherResult.data)

This allows for better naming through not repeating yourself, sometimes

const fetched = await $try(fetchItems())
if (fetched.error) return handleError(fetched.error)

const transformed = await $try(transform(fetched.data))
if (transformed.error) return handleError(transformed.error)

Is less repetitive to write than

const [fetched, fetchedError] = await $try(fetchItems())
if (fetchedError) return handleError(fetchedError)

const [transformed, transformedError] = await $try(transform(fetched.data))
if (transformedError) return handleError(transformedError)

There is an argument about semantics and line width here as well, but I'm way too hungover to remember

data is possibly null

after handling the error and we are sure that the data is not null anymore, typescript cannot detect this and considers the data nullable. ts is still thinking about [null, error]

I was curious if it could be fixed though it is a typescript limit

const fetchData = () => Promise.resolve({id: 1})
const [data, error] = await trytm(fetchData())
if (error) {
  return
}

data.id // data is possibly null 

Fix Spelling on License File

The License file is not being picked up by GitHub or NPM due to the misspelling of the file in the root of the repo.

Package types are wrong

Hi, first thanks for making this!

I just tried it but it seems the types that are transpiled in the packags look like this:

declare const trytm: <T>(
   promise: Promise<T>,
) => Promise<readonly [Awaited<T>, null] | readonly [null, unknown]>;

export { trytm };

And the part that breaks is the | readonly [null, unknown]>

Because now if I try to use the package

const [data, error] = await trytm(myPromise())

error is unknown so even if I check if error !== null afterwards, the data type is not narrowed to not be null.

When copy/pasting the source in a helper directly in project it works alright.

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.