Nuxt/packages/utils/src/task.js

41 lines
922 B
JavaScript
Raw Normal View History

export const sequence = function sequence (tasks, fn) {
2018-12-22 21:05:13 +00:00
return tasks.reduce(
(promise, task) => promise.then(() => fn(task)),
Promise.resolve()
)
}
export const parallel = function parallel (tasks, fn) {
2018-12-22 21:05:13 +00:00
return Promise.all(tasks.map(fn))
}
export const chainFn = function chainFn (base, fn) {
2018-12-22 21:05:13 +00:00
if (typeof fn !== 'function') {
return base
}
if (typeof base !== 'function') {
return fn
}
return function (arg0, ...args) {
const next = (previous = arg0) => {
const fnResult = fn.call(this, previous, ...args)
if (fnResult && typeof fnResult.then === 'function') {
return fnResult.then(res => res || previous)
}
return fnResult || previous
2018-12-22 21:05:13 +00:00
}
const baseResult = base.call(this, arg0, ...args)
if (baseResult && typeof baseResult.then === 'function') {
return baseResult.then(res => next(res))
2018-12-22 21:05:13 +00:00
}
return next(baseResult)
2018-12-22 21:05:13 +00:00
}
}