TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Folding Promises in JavaScript

60 pointsby anon335dtzbvcover 7 years ago

10 comments

adamjcover 7 years ago
&gt;How can we make it better ? Let&#x27;s start by removing the requirement for identity value to always be the promise.<p>I challenge the view that making the identity value being able to be something other than a Promise is &#x27;making it better&#x27;. Pointless abstraction is one of my pet peeves in this industry. This looks like it has gone from a fairly straightforward, if kludgy, piece of code to something far more complex. Why not just:<p><pre><code> const listOfPromises = [...] const result = Promise.all(listOfPromises).then(results =&gt; { return results.reduce((acc, next) =&gt; acc + next) }) ?</code></pre>
评论 #15302580 未加载
评论 #15303830 未加载
noelwelshover 7 years ago
I don&#x27;t think this is very well written. It doesn&#x27;t start with any motivating problem, it introduces terms (functor) without defining them, and a lot of what is discussed doesn&#x27;t apply to solving the problem.
fair24over 7 years ago
&gt; Folding Promises in JavaScript<p>Or: How to make simple things complex and make a codebase a complete puzzle for those that come after you?
评论 #15302527 未加载
评论 #15303583 未加载
评论 #15303377 未加载
CapacitorSetover 7 years ago
I can&#x27;t quite understand the difference between endomorphism (&quot;input and output of the transformer must be from the same category&quot;) and homomorphism (&quot;structure preserving transformation. We always stay in the same category&quot;). Can someone help?
评论 #15302682 未加载
评论 #15302504 未加载
评论 #15302524 未加载
molfover 7 years ago
With async&#x2F;await this can become:<p><pre><code> const reduceP = async (fn, identity, listP) =&gt; { const values = await Promise.all(listP) return values.reduce(fn, identity) } </code></pre> The whole thing feels like a synthetic and overcomplicated example, though. In practice I&#x27;m sure I&#x27;d just write:<p><pre><code> let total = 0 while (listP.length &gt; 0) { total += await listP.pop() }</code></pre>
评论 #15302589 未加载
egeozcanover 7 years ago
I don&#x27;t know much about these concepts but isn&#x27;t `const objToArray = ({ a }) =&gt; [a];` losing data, that being the key of the value in the object? I&#x27;m asking because it says that &quot;Isomorphism is a pair of transformations between two categories with no data loss&quot;.<p>In any case, this is very helpful, thanks for writing&#x2F;sharing.
评论 #15302452 未加载
评论 #15302474 未加载
评论 #15302449 未加载
评论 #15303477 未加载
fortythirteenover 7 years ago
&quot;Programs must be written for people to read, and only incidentally for machines to execute.&quot; - Harold Abelson
porluneover 7 years ago
The author mentions the library Bluebird, which I think is a fantastic library. The &#x27;mapSeries&#x27; method it offers is also very useful when iterating over an array of values that need to be &#x27;promisified&#x27; and mapped in the given order. You can even set &#x27;concurrency&#x27; as an option, which puts a limit on the concurrent promises that can run (great for reducing API load).
chajathover 7 years ago
I&#x27;ve written a javascript library to deal with folding and mapping recurring promises (i.e. promises that resolve to a value part of which contains clue to the &quot;next&quot; promise)<p><a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;chained-promise" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;chained-promise</a>
minitechover 7 years ago
With async (it’s just monads!):<p><pre><code> listOfPromises.reduce( async (m, n) =&gt; await m + await n, 0, )</code></pre>