Thursday, March 19, 2015

Oops: Javascript: wrapping sync calls in a promise to they can be chained with async calls

As it turns out, I wasn't completely right in my previous post: http://jiffie.blogspot.nl/2015/02/javascript-wrapping-sync-calls-in.html.

Apparently it's not necessary to write :

// And now we create a promise from this
// instruction so we can chain it
var deferred = $.Deferred();
deferred.resolve(instruction);
return deferred.promise();

Why? I hear you ask? Because of the Monad pattern.
What's a monad? Good question. Read the book this guy wrote on Stack Overflow - http://stackoverflow.com/a/28947696/896697 - where he takes my answer and explains why I shouldn't have written it that way ...


No comments:

Post a Comment