News for hackers. Stuff that matters.
Re:Show HN: Cancelable async primitives for JavaScript by Mitranim on Wednesday August 09, 2017 @06:06AM (#14967084)
For one operation, sure. It doesn't work when you want to compose multiple async operations, wait until all are finished, or race several of them to completion (e.g. useful operation competing against timeout). The purpose of promises/futures is this composability that allows you to get the order and timing of operations right.
Re:Show HN: Cancelable async primitives for JavaScript by z3t4 on Wednesday August 09, 2017 @06:54AM (#14967336)
What if you want to abort, or take another path, depending on an error code ? With callbacks there's a convention that the first parameter is either null or an error.
Re:Show HN: Cancelable async primitives for JavaScript by Mitranim on Wednesday August 09, 2017 @08:51AM (#14968118)
That's what the mapping operators are for: `.mapResult` (same as `promise.then`), `.mapError` (same as `promise.catch`), or `.map`. The latter has an errback signature, like Node.js callbacks. They can also return new futures, transforming the result asynchronously. On top of that, error handling in promises/futures is much easier than in callbacks, as you can use one error handler for a chain of operations. Kinda like exceptions in synchronous code.