3 ms·
I really like chaining Promises in JavaScript. The Python way to do this doesn't seem as powerful (what if you want to inject a parallel set of async operations
by jondubois 9y ago
I really like chaining Promises in JavaScript. The Python way to do this doesn't seem as powerful (what if you want to inject a parallel set of async operations at some point in the chain?): https://stackoverflow.com/questions/43325501/how-do-i-write-a-sequence-of-promises-in-python https://stackoverflow.com/questions/43325501/how-do-i-write-... - JS also supports async/await but the advantage of JS is that you can return promises (or parallel combinations of multiple promises) from inside promises so any link in the promise chain can essentially inject new links in the chain in its own place (so the chain can change dynamically based on the results of previous async operations).
You can build really complex dynamic async chains in JS very easily because of this. It's hard to explain why it's so nice until you try it.
In JS, I can do something like:
Promise.resolve().then(() => {
let innerPromiseChain = serialOperation1()
.then(serialOperation2);
if (somethingIsTrue) {
// Only inject serialOperation3 at the end of the
// innerPromiseChain if somethingIsTrue
return innerPromiseChain.then(serialOperation3);
}
// else return the inner chain without serialOperation3
return innerPromiseChain;
})
.then((resultOfLastSerialOperation) => {
// resultOfLastSerialOperation could be the result of
// either serialOperation2 or serialOperation3 depending
// on the value of somethingIsTrue
// parallelOperation1 and parallelOperation2 will be
// executed in parallel.
return Promise.all(parallelOperation1(), parallelOperation2());
})
.then((parallelOp1Result, parallelOp2Result) => {
// This will be called once the previous parallel
// operations have both finished (therefore the
// whole chain has been processed).
});
- crimsonalucard 9y agoYou realize your promise code is ugly af. All your code is nested within other functions and chained with dots. It's not clear. For imperative code the goal is to write code that is just as clear as writing a single threaded algorithm: a simple list of steps. Promises and callbacks DO NOT ACHIEVE THIS GOAL. That stackoverflow post showed a python solution that's infinitely cleaner than js. Python skipped callbacks and promise chains and jumped directly to the best way to do async operations... async/await. You want to shove a parallel set in an async chain of operations? Here's the way to do serial and parallel async operations in Python: async def main(): # run them sequentially (but the loop can do other stuff in the meanwhile) body1 = await get_body('http://httpbin.org/ip') print body1 body2 = await get_body('http://httpbin.org/user-agent') print body2 # run them in parallel task1 = get_body('http://httpbin.org/ip') task2 = get_body('http://httpbin.org/user-agent') for body in await asyncio.gather(task1, task2): print(body) You see that code? Way more clearer way more understandable than your promise chain. Python has it's problems but your promise code is not powerful at all, it leads to over complicated code and just slightly better than callbacks.
- cryptica 9y agoWell put. Good to see that Python can handle this cleanly. You can do pretty much exactly the same with JS but I do think that there is some merit to this intermediate step of having the then() clauses for backwards-compatibility because you can't always guarantee that the caller will in be an async function. Async/await is an all-or-nothing improvement not an incremental one.