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.

Chaining of async functions

See original GitHub issue

Hi again!

I am trying to find a readable way of chaining when using promises to write it as .then(doSomething) Suppose, we have a sequence of async functions fn1 and fn2.

const fn1 = (a: number) => Promise.resolve(right(a + 1));
const fn2 = (b: number) => Promise.resolve(right(b + 2));

My first idea was to write something like:

const fn = () =>
  Promise.resolve(right(1))
    .then((mb1) => mb1.asyncChain(fn1))
    .then((mb2) => mb2.asyncChain(fn2));

To hide the callbacks, I wrapped the functions with Either.asyncChain and obtained something like:

const fn1 = (mb: Either<Error, any>) => mb.asyncChain((a: number) => Promise.resolve(right(a + 1)));
const fn2 = (mb: Either<Error, any>) => mb.asyncChain((b: number) => Promise.resolve(right(b + 2)));

const fn = () =>
  Promise.resolve(right(1))
    .then(fn1)
    .then(fn2);

Is there a simpler way to chain the functions? Is it possible to implement some new method in Either to chain like right(1).thenChain(fn1).thenChain(fn2)?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
JSMonkcommented, Jan 10, 2021

I will publish it this evening. Sorry for the so long implementation. And yes, in the duration of compilation I will delete ClassImplements to reduce runtime cost.

1reaction
JSMonkcommented, Jan 26, 2021

Yes, sorry. It’s a bug. Already fixed it in 2.2.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to chain asynchronous functions in JavaScript
This can be done by asynchronous code like promises or async functions (which basically are cleaner promises). Asynchronous functions are cool, ...
Read more >
Chain async functions - javascript - Stack Overflow
Is it possible to chain calls with async functions, or do you have to assign a new variable for each result? Don't know...
Read more >
How to chain asynchronous functions in JavaScript?
We can use the asynchronous programming provided by JavaScript that performs functions without blocking other operations of the thread. This can ...
Read more >
Chaining Async Functions like a Boss - Thoughtbot
Chaining asynchronous functions is a common problem across languages. JavaScript has Promise to deal with this, Elm has Task , and the list ......
Read more >
Promise chaining is dead. Long live async/await
When incorporating promise chaining into their code, developers create new functions every time there's a then() call. This takes up more memory ...
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