Git Product home page Git Product logo

Comments (9)

JFGHT avatar JFGHT commented on May 22, 2024 3

@lucaspoffo, @bezreyhan

How about this #46?

from await-to-js.

rizaldirnm avatar rizaldirnm commented on May 22, 2024 1

use res!.data.everything
to get safe deeply access object, read more detail here https://codewithstyle.info/Deep-property-access-in-TypeScript/

from await-to-js.

bezreyhan avatar bezreyhan commented on May 22, 2024 1

@lucaspoffo I have not tried this myself but it looks like conditional types with arrays should be possible: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#example-2

from await-to-js.

k1ngbanana avatar k1ngbanana commented on May 22, 2024 1
async function testA() {
  const result = await to(successAsync())
  if (result[0]) {
    console.log('Found error in successAsync')
    return // don't forget
  }
  console.log(result[1].value) // No type error
}

async function testB() {
  const [err, result] = await to(successAsync())
  if (err) {
    console.log('Found error in successAsync')
    return
  }
  console.log(result.value) // Type error
}

so is there any why to separate into two variables and show no err? the res[0] and res[1] is not quite clean for each time to evoke this function.

from await-to-js.

bezreyhan avatar bezreyhan commented on May 22, 2024

@Rizaldinurm using ! tells TS to ignore the type so when you use ! you lose type safety.

The ideal situation is that TS makes sure you have checked that error is undefined (or that res is defined) before using res.

from await-to-js.

lucaspoffo avatar lucaspoffo commented on May 22, 2024

@bezreyhan When the return type is an Array you cannot infer the values between each other,
but if the return type is an Object you can do it, like this:

async function to<T, U = Error>(promise: Promise<T>): Promise<{ error: U; data: undefined } | { error: null; data: T }> {
  return promise.then((data: T) => ({ error: null, data }))
    .catch((error: U) => ({ error, data: undefined }))
}


async function successAsync(): Promise<{ value: number }> { return { value: 5 } }

async function test() {
  const result = await to(successAsync())
  if (result.error) {
    console.log('Found error in successAsync')
    return // don't forget
  }
  console.log(result.data.value) // No type error
}

test()

from await-to-js.

lucaspoffo avatar lucaspoffo commented on May 22, 2024

@bezreyhan You're right, arrays works too, but you can't separate in 2 variables:

async function to<T, U = Error>(promise: Promise<T>): Promise<[U, undefined] | [null, T]> {
  return promise.then<[null, T]>((data) => ([null, data]))
    .catch<[U, undefined]>((error: U) => [error, undefined])
}

async function successAsync(): Promise<{ value: number }> { return { value: 5 } }

async function testA() {
  const result = await to(successAsync())
  if (result[0]) {
    console.log('Found error in successAsync')
    return // don't forget
  }
  console.log(result[1].value) // No type error
}

async function testB() {
  const [err, result] = await to(successAsync())
  if (err) {
    console.log('Found error in successAsync')
    return
  }
  console.log(result.value) // Type error
}

from await-to-js.

bezreyhan avatar bezreyhan commented on May 22, 2024

@JFGHT I haven't tested it but that look good to me!

from await-to-js.

mrmartineau avatar mrmartineau commented on May 22, 2024

I think this can now be closed because v3.0.0 with the fixes in #46 was released yesterday.

from await-to-js.

Related Issues (20)

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.