I've read this article:
Callbacks are imperative, promises are functional: Node’s biggest missed opportunity
There's a code:
var p1 = new Promise();
p1.then(console.log);
p1.resolve(42);
var p2 = new Promise();
p2.resolve(2013);
p2.then(console.log);
// prints:
// 42
// 2013
This makes sense to me. Very declarative code.
However, once I really use promise
in node.js
doing
npm bluebird
Here is the actuall code that does work:
var Promise = require('bluebird');
var r1;
var p1 = new Promise(function(resolve){
r1 = resolve;
});
p1.then(console.log);
r1(42);
var r2;
var p2 = new Promise(function(resolve){
r2 = resolve;
});
r2(2013);
p2.then(console.log);
To me the former code looks more reasonable. What is going on?
Any idea? Thanks.