Chaining of async functions
See original GitHub issueHi 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:
- Created 3 years ago
- Comments:16 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.Yes, sorry. It’s a bug. Already fixed it in
2.2.2