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.

API improvement ideas (usable behind @beta flag on npm)

See original GitHub issue

Just writing this here publicly to see if others agree or disagree.

I think it would be nice to have a utility function that has the following signature:

Promise<Result<T, E>> => ResultAsync<T, E>

Since sometimes it’s easier to write a function using async/await syntax.


Additionally, I am also thinking it would be nice to have another utility function with the following signature:

Result<T, E> => ResultAsyncT, E>

I guess the above would be called intoResultAsync or something like that?

Curious to hear what folks think.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
supermacrocommented, Oct 1, 2020

Released a combine utility function behind a @beta flag on npm!

Docs on combine: https://github.com/supermacro/neverthrow/releases/tag/v2.8.0-beta.0

Source code: https://github.com/supermacro/neverthrow/blob/master/src/utils.ts

Feedback always welcome!

1reaction
paduccommented, Nov 13, 2020

Here’s an example right now that I think justifies having a Promise<Result<T, E>> => AsyncResult<T, E> function:

I personally never use Promise<Result> in my code because if there’s a Result inside a function, it means that I have control over what’s happening inside and can make that function return a ResultAsync. Also, once you have a Promise, the caller will have to await it and you push the untyped error problem up the call chain. My principle is to transform the Promises into ResultAsync at the lowest level (third-party api / io) and use ResultAsync everywhere in my code.

For your example, in which I guess your point is to call both getSingleSite and getSiteComments simultaneously, I would have written

const getSiteAndComments = (siteId: string): ResultAsync<SiteAndComments, RouteError> => {
  return ResultAsync.fromPromise(Promise.all([
    getSingleSite(siteId),
    getSiteComments(siteId),
  ])).andThen(([siteResult, commentsResult]) => {
    if (commentsResult.isErr()) {
      return commentsResult
    }
    if (siteResult.isErr()) {
      return siteResult
    }

    return ok({
      site: siteResult.value,
      comments: commentsResult.value,
    })
}

Maybe we could add a utility equivalent to Promise.all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

launchdarkly-api - npm
This REST API is for custom integrations, data export, or automating your feature flag workflows. DO NOT use this client library to include ......
Read more >
learning-zone/nodejs-basics: Node.js Basics ( v18.x ) - GitHub
js for server-side coding. V8 is the JavaScript engine i.e. it parses and executes JavaScript code. The DOM, and the other Web Platform...
Read more >
GitLab Runner feature flags
Feature flag Default value Deprecated FF_CMD_DISABLE_DELAYED_ERROR_LEVEL_EXPANSION false No FF_NETWORK_PER_BUILD false No FF_USE_LEGACY_KUBERNETES_EXECUTION_STRATEGY false No
Read more >
Changelog | Meteor API Docs
Node.js security update to 12.21.0; meteor create my-app --plan professional new flag plan to enable you to choose a plan from the deploy...
Read more >
Kubernetes 1.23: Pod Security Graduates to Beta
kind create cluster --image kindest/node:v1.23.0 ... The best way to confirm the API's default enabled plugins is to check the Kubernetes ...
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