events#once JavaScript Examples

The following examples show how to use events#once. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: cache.js    From Adachi-BOT with MIT License 6 votes vote down vote up
async function doCache(url, dir) {
  const filepath = getCachedPath(url, dir);

  if (!(await isCached(url, dir))) {
    try {
      const stream = fs.createWriteStream(filepath);
      const writeFilePromise = util.promisify(fs.writeFile);

      await fetch(url)
        .then((r) => r.arrayBuffer())
        .then((r) => writeFilePromise(filepath, Buffer.from(r)))
        .then(() => stream.end());
      await once(stream, "finish");
    } catch (e) {
      global.bots.logger.error(`错误:拉取 ${url} 失败,因为“${e}”。`);
      return undefined;
    }
  }

  return fs.existsSync(filepath) ? filepath : undefined;
}