Skip to content

Instantly share code, notes, and snippets.

@codelahoma
Last active August 29, 2015 14:25
Show Gist options
  • Save codelahoma/71d648a3b37061e4d15d to your computer and use it in GitHub Desktop.
Save codelahoma/71d648a3b37061e4d15d to your computer and use it in GitHub Desktop.
promiseForPath - function for use with Q library
// 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