Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

deno-denops-defer

deno land

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.

Example

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],
}));