mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 17:43:59 +00:00
38 lines
962 B
JavaScript
38 lines
962 B
JavaScript
|
export const sequence = function sequence(tasks, fn) {
|
||
|
return tasks.reduce(
|
||
|
(promise, task) => promise.then(() => fn(task)),
|
||
|
Promise.resolve()
|
||
|
)
|
||
|
}
|
||
|
|
||
|
export const parallel = function parallel(tasks, fn) {
|
||
|
return Promise.all(tasks.map(fn))
|
||
|
}
|
||
|
|
||
|
export const chainFn = function chainFn(base, fn) {
|
||
|
/* istanbul ignore if */
|
||
|
if (typeof fn !== 'function') {
|
||
|
return base
|
||
|
}
|
||
|
return function () {
|
||
|
if (typeof base !== 'function') {
|
||
|
return fn.apply(this, arguments)
|
||
|
}
|
||
|
let baseResult = base.apply(this, arguments)
|
||
|
// Allow function to mutate the first argument instead of returning the result
|
||
|
if (baseResult === undefined) {
|
||
|
baseResult = arguments[0]
|
||
|
}
|
||
|
const fnResult = fn.call(
|
||
|
this,
|
||
|
baseResult,
|
||
|
...Array.prototype.slice.call(arguments, 1)
|
||
|
)
|
||
|
// Return mutated argument if no result was returned
|
||
|
if (fnResult === undefined) {
|
||
|
return baseResult
|
||
|
}
|
||
|
return fnResult
|
||
|
}
|
||
|
}
|