{"id":19545414,"url":"https://github.com/forter/few","last_synced_at":"2026-03-08T20:32:31.084Z","repository":{"id":57234858,"uuid":"49948905","full_name":"forter/few","owner":"forter","description":"Write fewer lines of code by turning node-style asynchronous functions and promises to a synchronous code using ES6 generators","archived":false,"fork":false,"pushed_at":"2023-03-21T19:27:22.000Z","size":50,"stargazers_count":52,"open_issues_count":8,"forks_count":3,"subscribers_count":55,"default_branch":"master","last_synced_at":"2025-04-11T18:54:34.913Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/forter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-19T11:42:16.000Z","updated_at":"2023-12-29T09:57:21.000Z","dependencies_parsed_at":"2022-08-23T16:20:28.917Z","dependency_job_id":null,"html_url":"https://github.com/forter/few","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forter%2Ffew","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forter%2Ffew/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forter%2Ffew/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forter%2Ffew/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/forter","download_url":"https://codeload.github.com/forter/few/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251041479,"owners_count":21527203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T03:38:34.474Z","updated_at":"2026-03-08T20:32:31.030Z","avatar_url":"https://github.com/forter.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# few [![Build Status](https://travis-ci.org/forter/few.svg?branch=master)](https://travis-ci.org/forter/few)\nWrite fewer lines of code by turning node-style asynchronous functions and promises to a synchronous code using ES6 generators.\n## Requirements\nfew is an npm module intended to run on node.js 4.0.0 and higher.  \nThis package is continuously tested on all minor versions from node.js 4.0.0 and higher using Travis CI.\n\n## Installation\nUsing npm:\n```bash\nnpm install few\n```\nUsing bower:\n```bash\nbower install few\n```\nSupporting all modern browsers using [Babel](https://babeljs.io/).\n\n## Example\n```javascript\nconst few = require('few');\n\nfunction returnValue(v, callback) {\n  process.nextTick(() =\u003e callback(null, v));\n}\n\nfunction* generateValue(v) {\n  return yield cb =\u003e returnValue(v, cb);\n}\n\n// Multiple invocations of few run asynchronously\n\nfew(function* () {\n  // Yield or delegate directly\n  const a = yield cb =\u003e returnValue(1, cb);\n  const b = yield Promise.resolve(2);\n  const c = yield 3;\n  const d = yield* generateValue(4);  // yield generateValue(4); Also works\n\n  // Prints 1 2 3 4\n  console.log(a, b, c, d);\n});\n\nfew(function* () {\n  // Parallelize using arrays\n  const arr = yield [\n    cb =\u003e returnValue(1, cb),\n    Promise.resolve(2),\n    3,\n    generateValue(4)\n  ];\n\n  // Prints [ 1, 2, 3, 4 ]\n  console.log(arr);\n});\n\nfew(function* () {\n  // Parallelize using objects\n  const obj = yield {\n    a: cb =\u003e returnValue(1, cb),\n    b: Promise.resolve(2),\n    c: 3,\n    d: generateValue(4)\n  };\n\n  // Prints { a: 1, b: 2, c: 3, d: 4 }\n  console.log(obj);\n});\n```\n## Usage\n### few(genOrFn[, callback])\n`genOrFn` must be an initialized generator or a generator function that does not expect any arguments. Any other type will produce a `TypeError`.  \n`callback`, if provided, must be a node-style callback, i.e. accepting an error and a result as arguments.  \nThe return value of the generator will be provided as the result argument and if an error is thrown, it will be provided as the error argument.  \nIf `callback` is not provided, any error that the generator produces, will be thrown.\n\n### Yieldable Objects\nfew supports the following types to be yielded:\n- Single node-style callback argument functions (aka thunks)\n- Promises\n- Simple values, which will be returned as-is\n- Arrays combining thunks, promises, generators or simple values to be run in parallel\n- Objects containing thunks, promises, generators or simple values to be run in parallel\n\n### Parallelization\nfew allows parallelization by yielding an array or an object.  \nThe yielded object or array may contain any combination of:\n- Single node-style callback argument functions (aka thunks)\n- Promises\n- Initialized generators\n- Simple values, which will be returned as-is\n\nWhen all given elements have finished processing, a new object or array that contains the results of the given elements in the same order will be returned.  \nIf any of the elements provides an error, the error will be thrown inside the generator.\n\n### Generator Delegation Support\nDelegation is supported using the `yield*` expression.  \nTo run in parallel, a generator can be passed as an element of the yielded array.\n\n### Error Handling\nAny error originating from yielded objects will be thrown inside the generator, and can be caught using `try...catch`.  \nFor example, the following code prints ERROR to stderr:\n```javascript\nfew(function* () {\n  try {\n    yield Promise.reject(new Error('ERROR'));\n  } catch (err) {\n    console.error(err.message);\n  }\n});\n```\nIf an error is thrown inside a generator (and not caught), it will be passed to the callback given as a second argument to `few` or thrown if a callback has not been given.  \nThe following example also prints ERROR to stderr:\n```javascript\nfew(function* () {\n  yield Promise.reject(new Error('ERROR'));\n}, (err, result) =\u003e { console.error(err.message); });\n```\nIn the following example, the uncaught error will crash the process. Make sure you handle all errors!\n```javascript\nfew(function* () {\n  yield Promise.reject(new Error('ERROR'));\n});\n```\n\n## License\nLicensed under Apache 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforter%2Ffew","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fforter%2Ffew","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforter%2Ffew/lists"}