question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ResultAsync is not a valid async function return type

See original GitHub issue

After a lot of research into error handling in TypeScript, I’ve pretty much settled on this library as being the best option for our use. However, I am running into a major blocker. My existing codebase is built using async functions and await, but after converting the return type of a method from a Promise<something> to ResultAsync<something, someError>, the compiler complains that

Type 'typeof ResultAsync' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

I am targeting ES5 in my tsconfig, because this is going to run in a browser, but it doesn’t make sense to me that it would not be promise compatible. This seems very basic so I figure I must be missing something here; is there a config option or something I may be overlooking? For the record, I’m using neverthrow 3.1.2.

As an example, even this basic method throws an error:

public async testAsync(): ResultAsync<string, Error> { return okAsync("Hello world!"); }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
supermacrocommented, Jan 6, 2021

Combining heterogeneous Results or ResultAsyncs has been on my todo-list for a while. In lieu of this missing functionality in neverthrow, this is how you could do it:

The combine function already does work for heterogeneous lists if you ran this code in javascript. The limitation right now is one of a simplified type definition - which forces you to only work on homogeneous lists in typescript only.

Proof: https://repl.it/@gDelgado14/heterogeneous-combine

So what you can do for now is provide typecasts on combine function invocations:

import { combine, Result } from 'neverthrow'

combine([ myOneResult, myOtherResult ]) as Result<[ Blah, Whatever ], Err1 | Err2>

If you want to get really fancy (which is what I have to implement anyways when I do get around to doing this) is to provide function overloads for all the various invocations of combine (just like how Promise.all is implemented in typescript)

2reactions
paduccommented, Jan 6, 2021

If both func1() and func2() return ResultAsync types that you need to compare, I would create a ResultAsync that has them both and then operate the comparison.

const res = func1().andThen((value1) => func2().map((value2) =>({ value1, value2 }))) // This combines the two values in a single composite value
                   .map(({ value1, value2}) => value1 == value2) // This does the comparison
Read more comments on GitHub >

github_iconTop Results From Across the Web

Type 'typeof ResultAsync' is not a valid async function return ...
I'm having trouble with my very first steps with this library. I'm trying something very simple like: async function foo(): Promise { return...
Read more >
Error: Type is not a valid async function return type in ES5/ES3 ...
An async function can ONLY return a promise by definition - all async functions return promises. It can't return a boolean.
Read more >
Type 'typeof ResultAsync' is not a valid async function return ...
I'm having trouble with my very first steps with this library. I'm trying something very simple like: async function foo(): Promise<number> ...
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly...
Read more >
C# Guide: Async Method Return Types Overview - Pluralsight
What's more, changing the return type of an async method can be contagious, as it were, in some cases requiring you to alter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found