Skip to content

Instantly share code, notes, and snippets.

@dmitrymatveev
Last active March 22, 2019 06:14
Show Gist options
  • Save dmitrymatveev/dca5343de3fe28376ff8f085a653bf20 to your computer and use it in GitHub Desktop.
Save dmitrymatveev/dca5343de3fe28376ff8f085a653bf20 to your computer and use it in GitHub Desktop.
export const pluck = (propPath, defaultValue = undefined) => {
let addr = !Array.isArray(propPath) ? `${propPath}`.split('.') : [...propPath];
return obj => {
let target = addr.reduce((res, prop) => (!isNullOrUndefined(res) ? res[prop] : res), obj);
return target === undefined ? defaultValue : target;
};
}
/**
* Run a seriese of pormises in a stack
*/
export const sequence = async (...promiseGenerators) => new Promise(function (resolve, reject) {
const HEAD = 0;
const step = async (lastResult, nextFunction, ...rest) => {
return await (Promise.resolve(nextFunction(lastResult)))
.then(res => {
const next = rest[HEAD];
if (next) return step(res, next, ...rest.slice(HEAD + 1));
else resolve(res);
})
.catch(reject);
};
return step(null, promiseGenerators[HEAD], ...promiseGenerators.slice(HEAD + 1));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment