3 ms·
Are promises basically streams? Or am I over-generalizing here?
by dizzystar 14y ago
Are promises basically streams? Or am I over-generalizing here?
- bad_user 14y agoYes and no. You can have stream-like functionality (piping the response of one promise to another), but that's not what promises are all about. A promise at its core represents a result that will be available in the future. The other thing you have to understand is that an instance of a promise supports monadic operations (i.e. filter, map, flatMap, depending on the implementation of course, but without these monadic operations any implementation is useless). What does this mean actually? It means that these promises are composable. Piping is only one usecase, however you can compose them in any way you want. For instance, you can do something like ... execute tasks A, B and C in parallel and from whichever one finishes first, from its result start tasks D1, D2 ... Dn in parallel, drop whichever of the tasks timeouts or throws an exception, then merge the results of the tasks that succeeded and pass it to task E, which will produce a final response.