【发布时间】:2020-08-26 04:50:04
【问题描述】:
我找到了一个 javascript 代码(我更新了),但有一部分代码我没有得到:/ (完整源代码:https://github.com/a-h/ddbimport/tree/working/01-nodeimport)
我不明白的部分代码:
const batchOf = (size = 25, execute) => {
const items = [];
return async (item, last = false) => {
if (item) {
items.push(item);
}
if (last || items.length === size) {
await execute(items);
items.length = 0;
}
};
};
首先,我不明白最后一项怎么可能是真的?我在我的 CSV 中尝试了 5 行,但它从来没有执行部分......
其次,javascript从哪里获取item本身的值?我的意思是我们通常需要声明item 或将其传递给函数的参数。因为如果当我尝试拆分异步函数本身时,我需要将参数 item 传递给我的函数。
例如:
const batchOf = (size = 25, execute) => {
const items = [];
async function passItem (item, last = false) {
if (item) {
items.push(item);
}
if (last || items.length === size) {
await execute(items);
items.length = 0;
}
};
return passItem(?, ? ); // i should pass some parameters here..
};
因此,如果有人可以帮助我解决 last 项目的问题,并向我解释 javascript 的魔力,因为我有点迷失了这个。
对不起,如果有重复的主题,但我不知道如何搜索。
感谢您的帮助。
【问题讨论】:
-
看看返回值:
return async (item, last = false) => {...}。batchOf存储一个函数,然后在第 92 行调用:const processor = batchOf(25, async (items) => {...})。
标签: javascript function logic