Code Monkey home page Code Monkey logo

until's People

Contributors

craig-davis avatar dependabot[bot] avatar karlhorky avatar kettanaito avatar mattcosta7 avatar mohamedwagih avatar nick-w-nick avatar singhamandeep007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

until's Issues

Question: Why use .catch on the promise

I am curious why this change was introduced.

// Original implementation  
export const until = async <DataType = unknown, ErrorType = Error>(promise: () => Promise<DataType>): Promise<[ErrorType, DataType]> => {
  try {
    const data = await promise()
    return [null, data]
  } catch (error) {
    return [error, null]
}
// Current implementation 
export const until = async <DataType = unknown, ErrorType = Error>(promise: () => Promise<DataType>): Promise<[ErrorType, DataType]> => {
  try {
   const data = await promise().catch((error) => { // .catch added
      throw error
    })
    return [null, data]
  } catch (error) {
    return [error, null]
}

Is there a benefit of calling .catch on the promise? It is my understanding that the catch block would handle throws?

Incorrect Types

Hi @kettanaito! ๐Ÿ‘‹ Hope you are well :)

The return type doesn't agree with the null values returned here:

export const until = async <DataType = unknown, ErrorType = Error>(promise: () => Promise<DataType>): Promise<[ErrorType, DataType]> => {
  try {
    const data = await promise().catch((error) => {
      throw error
    })
    return [null, data] // ๐Ÿ’ฅ Type 'null' is not assignable to type 'ErrorType'.
  } catch (error) {
    return [error, null] // ๐Ÿ’ฅ Type 'null' is not assignable to type 'DataType'.
  }
}

Created a playground for this: https://www.typescriptlang.org/play?ssl=1&ssc=1&pln=10&pc=2#code/AQQwzgngdgxsBmBXWAXAlgeysZ6A2wAPACIgogAqEADgKbAC8OUA1lBgO5QA0wAogCcBGAVTqN+QkQD4AFNWEBbNGFoAuYLICUjacAAKSlbRJlKNWtK0bDGZasIBtQcNEXepcmNoBdPcABvAChgYBQBCECQ0OAYLDAUYAATMwkQDhA0RIU7Y20AOhgyGAALWVlaKQEdBj1gmJiUEuEOYErXaNCAXy1O4AFaFEQBbEcoRDw8XhTyH2iu2OKSzXaRHXqYgaGR4EdVgV5xybnuoK6gA

Revert to array return type

The old versions of TS had trouble creating a discriminated union type out of an array tuple if you destructure it. That's not the case for the latest TS 4.9. We can use the array-based pattern again.

Alternative Tip/Suggestion

I saw someone using this library and it was confusing to me at first glance of what it was doing.
Then i figured: Is until needed, are there alternative solutions? And there is...

This dose not do exactly the same thing until is doing, but you won't need any try/catch or dependencies with the Settle method.

const [{status, value}] = await Promise.allSettled([fetchUser(id)])

the output is either:

{ status: "fulfilled", value: user } // or
{ status: "rejected",  reason: Error('an error') }

So the signature changes a bit. instead of checking for an error object u would have to check if status is fulfilled or rejected

-  const [error, user] = await until(() => fetchUser(id))
-
-  if (error) {
-    return handleError(error)
-  }

+  const [{status, value: user}] = await Promise.allSettled([fetchUser(id)])
+
+  if (status === 'rejected') {
+    return handleError(error)
+  }

user.password = 'abc'
await user.$save()

Sry if this is not an issue or bug, just wanted to share some stuff, you can close this - shares

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.