by adamjc on 9/21/17, 12:01 PM
>How can we make it better ? Let's start by removing the requirement for identity value to always be the promise.
I challenge the view that making the identity value being able to be something other than a Promise is 'making it better'. 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:
const listOfPromises = [...]
const result = Promise.all(listOfPromises).then(results => {
return results.reduce((acc, next) => acc + next)
})
?
by noelwelsh on 9/21/17, 12:09 PM
I don't think this is very well written. It doesn't start with any motivating problem, it introduces terms (functor) without defining them, and a lot of what is discussed doesn't apply to solving the problem.
by fair24 on 9/21/17, 11:47 AM
> Folding Promises in JavaScript
Or: How to make simple things complex and make a codebase a complete puzzle for those that come after you?
by CapacitorSet on 9/21/17, 11:59 AM
I can't quite understand the difference between endomorphism ("input and output of the transformer must be from the same category") and homomorphism ("structure preserving transformation. We always stay in the same category"). Can someone help?
by molf on 9/21/17, 12:17 PM
With async/await this can become:
const reduceP = async (fn, identity, listP) => {
const values = await Promise.all(listP)
return values.reduce(fn, identity)
}
The whole thing feels like a synthetic and overcomplicated example, though. In practice I'm sure I'd just write:
let total = 0
while (listP.length > 0) {
total += await listP.pop()
}
by egeozcan on 9/21/17, 11:41 AM
I don't know much about these concepts but isn't `const objToArray = ({ a }) => [a];` losing data, that being the key of the value in the object? I'm asking because it says that "Isomorphism is a pair of transformations between two categories with no data loss".
In any case, this is very helpful, thanks for writing/sharing.
by fortythirteen on 9/21/17, 12:19 PM
"Programs must be written for people to read, and only incidentally for machines to execute." - Harold Abelson
by porlune on 9/21/17, 11:50 AM
The author mentions the library Bluebird, which I think is a fantastic library. The 'mapSeries' method it offers is also very useful when iterating over an array of values that need to be 'promisified' and mapped in the given order. You can even set 'concurrency' as an option, which puts a limit on the concurrent promises that can run (great for reducing API load).
by chajath on 9/21/17, 4:26 PM
I'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 "next" promise)
https://github.com/google/chained-promise
by minitech on 9/21/17, 1:36 PM
With async (it’s just monads!):
listOfPromises.reduce(
async (m, n) => await m + await n,
0,
)