Created
June 15, 2015 10:57
-
-
Save thefourtheye/fd9d735eca6cb88c156f 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
var array = ['yshepu', 'mmxhhb', 'qwertz', 'wvlgrs', 'knfght']; | |
var prefix = 'myPrefix'; | |
var Q = require('q'); | |
function exists(prefix, name) { | |
var names = ['myPrefix:mmxhhb', 'myPrefix:knfght']; | |
return Q(names.indexOf(prefix + ':' + name) + 1); | |
} | |
function getActive(prefix, array) { | |
return Q.allSettled(array.map(function (currentItem) { | |
return exists(prefix, currentItem); | |
})); | |
}; | |
function processWithReduce(res) { | |
return res.reduce(function (result, currentResult, idx) { | |
if (currentResult.state === 'fulfilled' && currentResult.value === 0) { | |
result.push(array[idx]); | |
} | |
return result; | |
}, []); | |
} | |
function processWithFilter(res) { | |
return res.filter(function (x) { | |
return x.state === "fulfilled" && x.value === 0; | |
}); | |
} | |
getActive(prefix, array).then(processWithReduce).then(console.log); | |
// [ 'yshepu', 'qwertz', 'wvlgrs' ] | |
getActive(prefix, array).then(processWithFilter).then(console.log); | |
// [ { state: 'fulfilled', value: 0 }, | |
// { state: 'fulfilled', value: 0 }, | |
// { state: 'fulfilled', value: 0 } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment