Last active
February 17, 2016 18:09
-
-
Save iaean/cbc00c5e7e9834db8626 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
var Promise = require('bluebird'); | |
function delayResolve(t) { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { resolve(t); }, t); }); } | |
function delayReject(t) { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { reject(t); }, t); }); } | |
// .map() vs .each() vs. mapSeries() vs. filter() | |
// [email protected] | |
// [email protected] | |
var p0 = Promise.resolve(delayResolve(200)); | |
p0.then(function() { | |
var p1 = delayResolve(500); | |
var p2 = delayReject(300); | |
var p3 = delayResolve(400); | |
return Promise.all([p1,p2,p3].map(function(p) { return p.reflect(); })) }) | |
.map(function(p) { | |
return p.isFulfilled() ? p.value() : null; }) | |
.then(function(r) { | |
console.log(JSON.stringify(r)); }); // -> [500,null,400] | |
/* Possible reflection issue in .each() ? */ | |
p0.then(function() { | |
var p1 = delayResolve(500); | |
var p2 = delayReject(300); | |
var p3 = delayResolve(400); | |
return Promise.all([p1,p2,p3].map(function(p) { return p.reflect(); })) }) | |
.each(function(p) { | |
return p.isFulfilled() ? p.value() : null; }) | |
.then(function(r) { | |
console.log(JSON.stringify(r)); }); | |
// -> [{"_bitField":268435457,"_settledValue":500}, | |
// {"_bitField":135266305,"_settledValue":300}, | |
// {"_bitField":268435457,"_settledValue":400}] | |
/* Same reflection issue than .each() ? */ | |
p0.then(function() { | |
var p1 = delayResolve(500); | |
var p2 = delayReject(300); | |
var p3 = delayResolve(400); | |
return Promise.all([p1,p2,p3].map(function(p) { return p.reflect(); })) }) | |
.filter(function(p) { | |
return p.isFulfilled() ? p.value() : null; }) | |
.then(function(r) { | |
console.log(JSON.stringify(r)); }); | |
// -> [{"_bitField":268435457,"_settledValue":500}, | |
// {"_bitField":268435457,"_settledValue":400}] | |
/* .mapSeries() untested because of throwing <not a function> error in node */ | |
p0.then(function() { | |
var p1 = delayResolve(500); | |
var p2 = delayReject(300); | |
var p3 = delayResolve(400); | |
return Promise.all([p1,p2,p3].map(function(p) { return p.reflect(); })) }) | |
.mapSeries(function(p) { | |
return p.isFulfilled() ? p.value() : null; }) | |
.then(function(r) { | |
console.log(JSON.stringify(r)); }); | |
// TypeError: p0.then(...).mapSeries is not a function | |
// at Object.<anonymous> (/.../b3.js:70:2) | |
// at Module._compile (module.js:410:26) | |
// at Object.Module._extensions..js (module.js:417:10) | |
// at Module.load (module.js:344:32) | |
// at Function.Module._load (module.js:301:12) | |
// at Function.Module.runMain (module.js:442:10) | |
// at startup (node.js:136:18) | |
// at node.js:966:3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment