Last active
August 29, 2015 14:25
-
-
Save codelahoma/71d648a3b37061e4d15d to your computer and use it in GitHub Desktop.
promiseForPath - function for use with Q library
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
// Works like underscore-contrib's getPath function. | |
// Takes a promise that will resolve to an object and a dot notation string specifying a path. | |
// | |
// Cannot know if path exists in resolved value - returned promise will reject with a TypeError if you | |
// specify a subkey of a nonexistent key, and undefined for a single | |
// nonexistent key. | |
function promiseForPath(promise, path) { | |
var k = path.split('.'); | |
return k.reduce(function(memo, item){ | |
return memo.get(item); | |
}, promise); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment