首页 > 解决方案 > lodash中的流函数问题

问题描述

我开始在一个新项目中工作,在那里我找到了 lodash 的函数,我在文档flow中看到了它的用途,但是在我的项目中,在下面的代码中,我在这里找到了函数末尾的内容是什么?flow([...])(state)(state)

module.exports = (async function published(state) {
  return flow([
    setColumnIndex('my_pay_table', 1, 'rate_mode', getColumn('pay_structure', 'pay_per_id', state)),
    setColumnIndex('my_pay_table', 1, 'rate_amount', getColumn('pay_structure', 'pay_rate', state)),
    setColumnIndex('my_wo_title_table', 1, 'user_id', buildArtifact(ownerAlias, 'user', 'id', 1)),
    setColumnIndex('my_wo_title_table', 1, 'date_added', Date.now() / 1000),
  ])(state);
});

谁能帮我?

标签: node.jslodash

解决方案


根据 lodash 文档,flow返回一个函数。在 JavaScript 中,可以在不执行函数的情况下返回函数。

我们可以将您提供的代码重构为以下内容

module.exports = (async function published(state) {
  // `func` here is a function
  const func = flow([
    setColumnIndex('my_pay_table', 1, 'rate_mode', getColumn('pay_structure', 'pay_per_id', state)),
    setColumnIndex('my_pay_table', 1, 'rate_amount', getColumn('pay_structure', 'pay_rate', state)),
    setColumnIndex('my_wo_title_table', 1, 'user_id', buildArtifact(ownerAlias, 'user', 'id', 1)),
    setColumnIndex('my_wo_title_table', 1, 'date_added', Date.now() / 1000),
  ]);
  // Here we execute that function with an argument `state`
  return func(state);
});

推荐阅读