使我的readPackageFiles符合"Parallel"功能 [英] Conforming my readPackageFiles to fit inside "Parallel" function

查看:118
本文介绍了使我的readPackageFiles符合"Parallel"功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I got a lot of help from this SO question around grabbing directories and searching files. I'm trying to conform my readPackageFiles function to use the Parallel function given to me; however, I'm struggling to it it work.

错误:

UnhandledPromiseRejectionWarning: TypeError: Cannot use 'in' operator to search for 'throws' in 1
    at readFile (/Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/jsonfile/index.js:22:16)
    at Promise (/Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/universalify/index.js:13:12)
    at new Promise (<anonymous>)
    at readFile (/Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/universalify/index.js:7:14)
    at Array.map (<anonymous>)
    at /Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/dist/index.js:1:1833
    at <anonymous>
(node:70097) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 63)
(node:70097) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
/Users/harwood/udev/dotcom-components/tools/dependency-version-comparison/node_modules/jsonfile/index.js:43
    callback(null, obj)

问题:如何使readPackageFiles与Parallel兼容?

Question: How do I conform readPackageFiles to work with Parallel?

import {readdir, stat, readJson} from 'fs-extra';
import {join, basename} from 'path';

const Parallel = p =>
  ({
    map: async f =>
      Promise.all((await p).map(f))
    ,
    filter: async f =>
      Promise.all((await p).filter(f))
    ,
    flatMap: async f =>
      Promise.all((await p).map(f)).then(ys => [].concat(...ys))
    ,
  });
const files = async (path = '.') =>
  (await stat(path)).isDirectory()
  ? Parallel(readdir(path))
    .flatMap(f => files(join(path, f)))
  : [path];

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

// How can I write this to use Parallel    
const readPackageFiles = async (packages) => await Promise.all(packages.map(async x => (await readJson(x))));

// this will not work but it's my attempt

const readPackageFilesNOTWORKING = async(path = ".") =>
  Parallel (search('package.json', path))
    .map (readJson)

用法:

search('package.json', '.')
  .then(readPackageFiles)
  .then(console.log);

推荐答案

我希望您能仔细研究链接的Q& A.我怀疑您需要这样的功能,并且此功能readPackages位于我与您共享的第一个链接中-

I was hoping you'd take a closer look at the linked Q&A's. I was suspecting you needed something like this and this function, readPackages, was in the first link I shared with you -

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)))
      )

readPackages (".") .then (console.log, console.error)
// [ ... ]

请注意,我的任何答案都不需要fs-extra.

Note, fs-extra is not required in any of my answers.

这篇关于使我的readPackageFiles符合"Parallel"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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