Skip to content

Instantly share code, notes, and snippets.

@rhogeranacleto
Last active May 15, 2017 12:15
Show Gist options
  • Save rhogeranacleto/39aaf17d09cf5216208c865795ba1a1f to your computer and use it in GitHub Desktop.
Save rhogeranacleto/39aaf17d09cf5216208c865795ba1a1f to your computer and use it in GitHub Desktop.
Implement Promise by object parameters
'use strict';
Object.defineProperties(Promise, {
when: {
configurable: false,
enumerable: false,
writable: false,
value: function name(hash) {
var $q = [];
var props = [];
for (let key in hash) {
if (hash.hasOwnProperty(key)) {
props.push(key);
$q.push(hash[key]);
}
}
return Promise.all($q).then(function (data) {
var obj = {};
for (let i = 0; i < data.length; i++) {
obj[props[i]] = data[i];
}
return obj;
});
}
}
});
/**
* Usage
*/
Promise.when({
first: Promise.resolve('first'),
second: Promise.resolve('second')
}).then(hash => {
console.log(hash.first); //first
console.log(hash.second); //second
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment