{"id":15706297,"url":"https://github.com/gimenete/pync","last_synced_at":"2025-05-12T18:47:43.606Z","repository":{"id":65371624,"uuid":"50195343","full_name":"gimenete/pync","owner":"gimenete","description":"Utilities for promises pync ~= promise + async","archived":false,"fork":false,"pushed_at":"2016-10-25T15:32:37.000Z","size":6,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-02T04:18:33.946Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gimenete.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-22T17:07:50.000Z","updated_at":"2019-07-15T21:00:22.000Z","dependencies_parsed_at":"2023-01-19T22:45:17.141Z","dependency_job_id":null,"html_url":"https://github.com/gimenete/pync","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gimenete%2Fpync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gimenete%2Fpync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gimenete%2Fpync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gimenete%2Fpync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gimenete","download_url":"https://codeload.github.com/gimenete/pync/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253802598,"owners_count":21966829,"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-10-03T20:22:13.413Z","updated_at":"2025-05-12T18:47:43.024Z","avatar_url":"https://github.com/gimenete.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pync\n\nUtilities for promises pync ~= promise + async\n\n`Promise.all()` is very useful but it runs all promises in parallel. Pync solves that.\n\nFor example we have a function like this:\n\n```javascript\nfunction somethingAsync(value) {\n  return new Promise((resolve, reject) =\u003e {\n    console.log('starting', value)\n    setTimeout(() =\u003e {\n      console.log('finishing', value)\n      resolve(`result ${value}`)\n    }, 100)\n  })\n}\n```\n\nThen we use `Promise.all()`:\n\n```javascript\nPromise.all([1, 2, 3].map((value) =\u003e somethingAsync(value)))\n  .then((results) =\u003e console.log('done', results))\n```\n\nWe will see:\n\n```\nstarting 1\nstarting 2\nstarting 3\nfinishing 1\nfinishing 2\nfinishing 3\ndone [ 'result 1', 'result 2', 'result 3' ]\n```\n\nThis means all promises are started at once. What if we want to run one after another? Imagine you have an array of URLs and you want to download the files pointed by them. You probably don't want to download all at once. `pync` solves that with these methods:\n\n## pync.series(arr, func)\n\n```javascript\nconst pync = require('pync')\n\nconst arr = [1, 2, 3]\npync.series(arr, (value) =\u003e somethingAsync(value))\n  .then(() =\u003e console.log('done'))\n```\n\nThis is the output:\n\n```\nstarting 1\nfinishing 1\nstarting 2\nfinishing 2\nstarting 3\nfinishing 3\ndone\n```\n\n## pync.map(arr, func)\n\n```javascript\nconst pync = require('pync')\n\nconst arr = [1, 2, 3]\npync.map(arr, (value) =\u003e somethingAsync(value))\n  .then((results) =\u003e console.log('done', results))\n```\n\nThis is the output:\n\n```\nstarting 1\nfinishing 1\nstarting 2\nfinishing 2\nstarting 3\nfinishing 3\ndone [ 'result 1', 'result 2', 'result 3' ]\n```\n\n## pync.dict(arr, func)\n\nGiven an array of strings it will call `func` for each string and finally construct an object with all the keys mapped to the values returned by `func`.\n\n```javascript\nconst pync = require('pync')\n\nconst arr = ['foo.txt', 'bar.txt']\npync.dict(arr, (filename) =\u003e readFileAsync(filename))\n  .then((results) =\u003e console.log('done', results))\n```\n\nThis is the output:\n\n```\ndone { 'foo.txt': 'contents of foo.txt', 'bar.txt': 'contents of bar.txt' }\n```\n\n## pync.whilst(test, func[, initialValue])\n\nIt executes a given function `func` whilst a given function `test` returns a truly value. The `test` function must be synchronous and it's always evaluated before `func`. You can pass an initial value, in which case both the `test` and `func` functions will receive it as an argument. Then, the value returned by `func` will be passed to the next iteration, so both `test` and `func` will receive it. And when finally `test` returns a falsy value, the latest value returned by `func` will be returned by `pync.whilst()`\n\n```javascript\nconst pync = require('pync')\n\nconst test = (totalAffectedRows) =\u003e totalAffectedRows \u003c 1000\nconst func = (totalAffectedRows) =\u003e db.someBatchUpdate().then((result) =\u003e result.affectedRows + totalAffectedRows)\npync.whilst(test, func, 0)\n  .then((totalAffectedRows) =\u003e console.log('total affected rows', totalAffectedRows))\n```\n\n# Index\n\nAll iteratees receive the current iteration number as second parameter.\n\n# Installing\n\n```bash\nnpm install pync --save\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgimenete%2Fpync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgimenete%2Fpync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgimenete%2Fpync/lists"}