TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Folding Promises in JavaScript

60 点作者 anon335dtzbvc超过 7 年前

10 条评论

adamjc超过 7 年前
&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 未加载
noelwelsh超过 7 年前
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.
fair24超过 7 年前
&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 未加载
CapacitorSet超过 7 年前
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 未加载
molf超过 7 年前
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 未加载
egeozcan超过 7 年前
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 未加载
fortythirteen超过 7 年前
&quot;Programs must be written for people to read, and only incidentally for machines to execute.&quot; - Harold Abelson
porlune超过 7 年前
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).
chajath超过 7 年前
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>
minitech超过 7 年前
With async (it’s just monads!):<p><pre><code> listOfPromises.reduce( async (m, n) =&gt; await m + await n, 0, )</code></pre>