Last active
May 15, 2017 12:15
-
-
Save rhogeranacleto/39aaf17d09cf5216208c865795ba1a1f to your computer and use it in GitHub Desktop.
Implement Promise by object parameters
This file contains 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
'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