2019-07-10 10:45:49 +00:00
|
|
|
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()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const parallel = function parallel (tasks, fn) {
|
2018-12-22 21:05:13 +00:00
|
|
|
return Promise.all(tasks.map(fn))
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const chainFn = function chainFn (base, fn) {
|
2018-12-22 21:05:13 +00:00
|
|
|
if (typeof fn !== 'function') {
|
|
|
|
return base
|
|
|
|
}
|
2020-09-09 09:22:43 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-09-09 09:22:43 +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
|
|
|
}
|
2020-09-09 09:22:43 +00:00
|
|
|
|
|
|
|
return next(baseResult)
|
2018-12-22 21:05:13 +00:00
|
|
|
}
|
|
|
|
}
|