defer
resolves combined multiple denops calls like gather
.
defer
preserves the structure of the complex object returned by the executor
and resolves Promise it contains.
To get expected
from the following input
:
const input = [
{ word: "foo" },
{ word: "hello" },
{ word: "🚀☄" },
];
const expected = [
{ word: "foo", bytes: 3 },
{ word: "hello", bytes: 5 },
{ word: "🚀☄", bytes: 7 },
];
Using defer
:
const output = await defer(denops, (helper) =>
input.map((item) => ({
...item,
bytes: strlen(helper, item.word) as Promise<number>,
})));
Using gather
(requires intermediate variable):
const intermediate = await gather(denops, async (helper) => {
for (const item of input) {
await strlen(helper, item.word);
}
}) as Promise<number[]>;
const output = input.map((item, index) => ({
...item,
bytes: intermediate[index],
}));