3 ms·
Not sure what you mean by "build up and tear down" but in what way do promises not pass errors to the next function in the chain? Consider code like this:
by chatmasta 8y ago
Not sure what you mean by "build up and tear down" but in what way do promises not pass errors to the next function in the chain?
Consider code like this:
p1
.then((value) => {
console.log(value);
return Promise.reject('oh, no!');
})
.catch((e) => {
console.log(e);
// Handle the error if you want, and keep going
return Promise.resolve()
})
.then(() => {
console.log('This still gets executed')
})
How is this not amenable to the railway pattern? You define how you handle the error within a function in the promise chain. Error handlers are fully composable with success handlers, which seems to be the important element necessary for implementing the railway pattern.
- joshribakoff 8y agoPromises are functors not monads because they map (.then) but the return value becomes the new promise. The chain is flat Rxjs streams are monads because you can map a stream to a stream which wraps the inner stream in a source stream, which you can then flatMap.
- matt_kantor 8y ago> Promises are functors not monads That's not quite true. `then` is a weird hybrid of `map` and `bind`. If its callback returns a Promise it acts like `bind` (you don't get a nested Promise), but otherwise it acts like `map`. And to be pedantic, Promise is neither a functor nor monad because `then` doesn't follow the necessary laws.