提交 3c8339fa 编写于 作者: U ULIVZ

Revert "revert: change AsyncOption to prototype-style."

This reverts commit a5cbd673.
上级 91d87207
......@@ -11,69 +11,69 @@ const Option = require('./Option')
* Expose Asynchronous Option.
*/
class AsyncOption extends Option {}
class AsyncOption extends Option {
/**
* Asynchronous serial running
* @param args
* @param {Array<AsyncFunction>} args
*/
/**
* Asynchronous serial running
* @param args
* @param {Array<AsyncFunction>} args
*/
AsyncOption.prototype.asyncApply = async function (...args) {
const items = []
for (const { name, value } of this.items) {
try {
items.push({
name,
value: isFunction(value)
? await value(...args)
: value
})
} catch (error) {
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
throw error
async asyncApply (...args) {
const items = []
for (const { name, value } of this.items) {
try {
items.push({
name,
value: isFunction(value)
? await value(...args)
: value
})
} catch (error) {
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
throw error
}
}
this.appliedItems = items
}
this.appliedItems = items
}
/**
* Asynchronous serial running
* @param args
* @param {Array<AsyncFunction>} args
*/
/**
* Asynchronous serial running
* @param args
* @param {Array<AsyncFunction>} args
*/
AsyncOption.prototype.parallelApply = async function (...args) {
const items = []
await Promise.all(this.items.map(async ({ name, value }) => {
try {
items.push({
name,
value: isFunction(value)
? await value(...args)
: value
})
} catch (error) {
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
async parallelApply (...args) {
const items = []
await Promise.all(this.items.map(async ({ name, value }) => {
try {
items.push({
name,
value: isFunction(value)
? await value(...args)
: value
})
} catch (error) {
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
throw error
}
})).catch(error => {
throw error
}
})).catch(error => {
throw error
})
return items
}
})
return items
}
/**
* Process a value via a pipeline.
* @param input
* @returns {*}
*/
/**
* Process a value via a pipeline.
* @param input
* @returns {*}
*/
AsyncOption.prototype.pipeline = async function (input) {
for (const fn of this.values) {
input = await fn(input)
async pipeline (input) {
for (const fn of this.values) {
input = await fn(input)
}
return input
}
return input
}
AsyncOption.prototype.apply = AsyncOption.prototype.asyncApply
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册