Last active
March 22, 2019 06:14
-
-
Save dmitrymatveev/dca5343de3fe28376ff8f085a653bf20 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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; | |
}; | |
} |
This file contains hidden or 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
/** | |
* 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)); | |
}); |
This file contains hidden or 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
util functions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment