Skip to content

Instantly share code, notes, and snippets.

@herzi herzi/params.js
Last active Aug 29, 2015

Embed
What would you like to do?
/**
* Turns: {
* keyOne: promiseOne,
* keyTwo: promiseTwo
* } into {
* keyOne: resolutionOne,
* keyTwo: resolutionTwo
* }
*
* (To avoid messing around with unclear array indices when calling different
* kinds of async ops; e.g. different database queries and some file system call)
*/
function params(object) {
var keys = Object.keys(object);
return Q.all(keys.map(function (key) {
return Q(object[key]);
})).then(function (values) {
var result = {};
if (values.length !== keys.length) {
throw new Error();
}
for (var i = 0; i < keys.length; i += 1) {
result[keys[i]] = values[i];
}
return result;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.