jeudi 14 mars 2019

Patterns for dealing with async APIs in TypeScript

How do you deal with this common pattern I’ve run into myself a bunch in typescript?

async RemoteCall(): Promise<Result> {
    var res: Result = await RPC()
    if (res) {
        return res
    }
}

Should I:

  1. Make Result a more robust type? (e.g. has data and errors attributes)
  2. Change the return value to Promise<Result | undefined>?
  3. Fill in Result with default values / 0 initialized?
  4. Something else? Monads?

I'm unfamiliar with current best practices surrounding both Javascript and Typescript, so any pointers would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire