映射并返回已解析的 Promise 数组的函数的名称? [英] Name for a function that maps and returns a resolved array of promises?

查看:23
本文介绍了映射并返回已解析的 Promise 数组的函数的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个愚蠢的问题,但我最近发现自己经常使用这种抽象:

Maybe this is a stupid ask but I recently found myself using this abstraction often:

async function giveMeAName(cbAsync, initValue) {
  return await Promise.all(
    initValue.map(cbAsync),
  );
}

问题:这是其他人的共同任务吗?如果有,它有名字吗?如果没有,也许只是部分意识到,那么它是否让您想起了什么?否则,我可以删除问题.

Question: Is this a common task to anyone else? If so, does it have a name? If not, maybe it's only partly realize, so does it remind you of anything? Otherwise, I can just delete the question.

目前我正在将这个函数用于这组指令.下面的代码将获取路径的所有目录并收集其中包含 package.json 的所有目录:

Currently I'm using this function for this set of instructions. Code below will get all directories of a path and collect all directories that have a package.json within:

const directories = (await getDirNamesByPath(rootPath));
const paths = await giveMeAName(addExistAdaptor, directories.map(joinPathWithName(rootPath)));
return await giveMeAName(collectJson, paths.filter(hasPath));

推荐答案

您几天前问了一个相关问题我试图帮助你,但你从来没有回复:(

You asked a related question a couple days ago that I tried helping you with, but you never replied :(

我已经回答了类似的问题(这里此处) 概括了这种模式 -

I've answered similar questions (here and here) that have generalised this pattern -

const Parallel = p =>
  ( { map: async f =>
        Promise .all ((await p) .map (x => f (x)))
    , filter: async f =>
        Promise .all ((await p) .filter (x => f (x)))
    , flatMap: async f =>
        Promise .all ((await p) .map (x => f (x))) .then (ys => [] .concat (...ys))
    , // ...
    }
  )

您可以看到它以这种方式与 files 一起使用,它递归地列出目录及其子目录中所有文件的所有路径 -

You can see it being used in this way with files, which recursively lists all paths to all files in a directory and its subdirectories -

const { readdir, stat } =
  require ("fs") .promises

const { join } =
  require ("path")

const files = async (path = ".") =>
  (await stat (path)) .isDirectory ()
    ? Parallel (readdir (path))
        .flatMap (f => files (join (path, f)))
    : [ path ]

还有一个专门化,search,它返回匹配查询的所有路径 -

And a specialisation, search, which returns all paths matching a query -

const { basename } =
  require ("path")

const search = async (query, path = ".") =>
  Parallel (files (path))
    .filter (f => basename (f) === query)

readPackages 递归读取指定路径下的所有 package.json 文件 -

And readPackages which recursively reads all package.json files at a specified path -

const { readFile } =
  require ("fs") .promises

const readPackages = async (path = ".") =>
  Parallel (search ("package.json", path))
    .map (readFile)
    .then
      ( buffers =>
          buffers .map (b => JSON .parse (String (b)))
      )

最后,一个稍微复杂一点的例子,dirs,它的工作原理类似于 files,但只递归地列出目录.递归的级别可以通过depth参数控制-

Finally, a slightly more complex example, dirs, which works like files but recursively lists directories only. The level of recursion can be controlled by the depth parameter -

const dirs = async (path = ".", depth = Infinity) =>
  (await stat (path)) .isDirectory ()
    ? depth === -1
        ? []
        : Parallel (readdir (path))
            .flatMap (f => dirs (join (path, f), depth - 1))
            .then (results => [ path, ...results ])
    : []

要查看没有Parallel 模块的这些程序的样子,请参阅上面链接的问答.

To see what these programs look like without the Parallel module, see the linked Q&A's above.

这篇关于映射并返回已解析的 Promise 数组的函数的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆