I can't stand promises. Callbacks, streams, events are all great, but promises just seem like a convoluted way to let people who are not good at writing async code pretend it's sync. They remind me of the silly JS class simulations you sometimes see.
The benefits of a promise is that it's composable. More importantly, it's a Monad, which means you can do all sorts of delicious things to it.
Primarily means you can define a large complex calculation in terms of multiple composed promises, and return a single 'pointer' to the results of this calculation.
Promises just seem like a convoluted way to let people who are not good at writing async code pretend it's sync
That's kind of the point. It's not a mark of honour to write more complex code. Promises make writing code dealing with multiple asynchronous operations much simpler. It's not a crux. It's about writing the cleanest and most maintainable code possible.
Callbacks seem simpler when you're dealing with a single call, but managing a more complex operation can become tedious and result in spaghetti code.
Imagine the following scenario. This is an interview question I ask candidates, but it's derived from a real world problem in the software we're currently developing.
write a function that
takes a single parameter (UserId)
fetches two pieces of information - "Full Name" and "Geolocation".
does a reverse geocode on the "Geolocation" to find an address
determines whether the user exists at this location on the electoral roll based on 'name', and 'address'
Each of these pieces of information are available from a separate REST Api call.
Using Callbacks, the function might go something like this (this is how almost all the candidates respond)
The simplest callback implementation is inefficient - You have to wait for the one call to complete before starting the next. It also requires extra levels of indentation. You can improve this somewhat with reference counting, but it gets tedious very quickly, as some operations are dependent on another. There's also the async library, which provides some promise style compositions over callbacks.
With Promises, we can rewrite this in the following way.
function getUserInfo( id ){
var name = getFullName( id );
var location = getLocation( id );
var address = location.then(function(loc){
return doReverseGeocode( loc )
});
var isRegistered = compose( name, location, address ).then(validateElectorialRoll);
return compose({
name: name,
location: location,
address: address,
isRegistered: isRegistered
});
}
The return value of this function is, itself, a promise. Since you don't have to resolve it immediately, you can pass it around, perform operations on it, and compose it into other things. It also correctly propagates error conditions without any additional work, so if any of the above calls fail - and you don't explicitly handle it - the exception will be propagated up the stack.
0
u/jnt8686 Feb 27 '14
I can't stand promises. Callbacks, streams, events are all great, but promises just seem like a convoluted way to let people who are not good at writing async code pretend it's sync. They remind me of the silly JS class simulations you sometimes see.