Skip to content

Instantly share code, notes, and snippets.

@iarna
Last active August 29, 2015 13:56
Show Gist options
  • Save iarna/9125808 to your computer and use it in GitHub Desktop.
Save iarna/9125808 to your computer and use it in GitHub Desktop.
// My Promisable's library provides two forms of use, one looks mostly like typical promises/A+ a'la Q and bluebird.
// The other looks more like callbacks. This gist shows examples of the two that are semantically equivalent.
// In practice, you wouldn't normally chain Promisables in the callback style to connect success and error handlers the way
// you do with the promises with then/catch/finally.
Assuming:
var Promisable = require('promisable');
Then:
.then(function(V){ … })
(function(E,V){ if (E) throw E; … })
ThenPromise:
.thenPromise(function(R){ … })
.then(function(V){ return Promisable(function(R){ … })})
(function(E,V){ if (E) throw E; return Promisable(function(R){ … })})
Catch:
.catch(function(E){ … })
(function(E){ if (!E) return; … })
Finally: (has no equivalent)
.finally(function(){ … })
// Promisable's handle errors some what more strictly then most promises, in that if your promisable is rejected
// and you don't deal with the error an exception will be thrown. eg
Promisable.reject(); // throws an exception at nextTick
Promisable.reject().catch(function(E){ … }); // ok
Promisable.reject()(function(E,V){ … }); // also ok
Promisable.reject().then(function(V){ … }); // throws an exception
Promisable.reject().then(function(V){ … }).catch(function(E){ … }); // ok
Promisable.reject().catch(function(E){ … }).then(function(V){ … }); // also ok, but weird
Promisable.fulfill().then(function(V){ return Promisable.reject() }); //throws an exception
Promisable.reject().finally(function(E,V){ … }); // throws an exception
Promisable.reject().catch(function(E){ … }).finally(function(E,V){ … }); // ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment