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

查看:117
本文介绍了映射并返回已解决的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))
    , // ...
    }
  )

您可以看到它以这种方式用于个文件,递归列出导向器中所有文件的所有路径y及其子目录-

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 ]

专门化的搜索,它返回与查询匹配的所有路径-

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天全站免登陆