Skip to content

Instantly share code, notes, and snippets.

@thefourtheye
Created June 15, 2015 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefourtheye/fd9d735eca6cb88c156f to your computer and use it in GitHub Desktop.
Save thefourtheye/fd9d735eca6cb88c156f to your computer and use it in GitHub Desktop.
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