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.

Add convenience method to create a ResultAsync from Result

See original GitHub issue

In non-Result(neverthrow)-land, using vanilla Promise we can easily convert a non-async method to be async using Promise.resolve('myString) which returns Promise<string>.

What is the equivalence using ResultAsync? I.e. I want to super easily convert a Result<T, E> to ResultAsync<T, E>.

I’ve managed to do it using this code - but feels like there must be (or should be) an easier way:

export const resultToAsync = <T, E>(
	result: Result<T, E>,
): ResultAsync<T, E> => {
	return result.asyncAndThen((value) => {
		return okAsync(value)
	})
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
supermacrocommented, Jan 6, 2021

@Sajjon what about using asyncMap?

const identity = <T>(val: T): T => val

export const resultToAsync = <T, E>(
	result: Result<T, E>,
): ResultAsync<T, E> => {
	return result.asyncMap(identity)
}

In fact, there’s no need to have a resultToAsync function at all:

myResult
  .map(doSomeSyncCryptoStuff)
  .andThen(doSomeSyncStuffThatMayFail)
  .asyncMap((val) => val) // now it's a ResultAsync

Lastly, you could also use asyncAndThen on the result to simultaneously:

  • Do an async computation
  • Convert the Result into a ResultAsync.
0reactions
supermacrocommented, Jan 8, 2021

I fail to see the point of Result => ResultAsync as all methods that accept a ResultAsync also accept a Result.

Not sure, just trying to find simple solutions for @Sajjon.

Going to close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AsyncThrowingStream | Apple Developer Documentation
AsyncThrowingStream conforms to AsyncSequence , providing a convenient way to create an asynchronous sequence without manually implementing an asynchronous ...
Read more >
Neverthrow - npm.io
errAsync convenience function to create a ResultAsync containing an Err type Result. import { ok, Ok, err, Err, Result, ...
Read more >
Best way to insert a method into a class with a bound variable
What I'm trying to build apart is the method "apply(tr:scala.util.Try)". This method can call the promise "result$async" to give the result of the ......
Read more >
c# - Converting convenience methods that use Tasks
You're trying to avoid making that "convenience method" async , but there's no reason to do that. What you want is to call...
Read more >
Ruby | Hypertable - Big Data. Big Performance
The following code snippets illustrate how to create a Thrift client object ... The following is example output produced by the above code...
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