{"_id":"async-foreach","_rev":"44344","name":"async-foreach","description":"An optionally-asynchronous forEach with an interesting interface.","dist-tags":{"latest":"0.1.3"},"maintainers":[{"name":"cowboy","email":""}],"time":{"modified":"2021-06-03T10:13:38.000Z","created":"2012-01-11T02:54:51.611Z","0.1.3":"2013-04-29T12:55:46.176Z","0.1.2":"2012-01-11T02:54:51.611Z"},"users":{"tunnckocore":true,"deepaknverma":true,"petersandor":true,"leduytien":true},"author":{"name":"\"Cowboy\" Ben Alman","url":"http://benalman.com/"},"repository":{"type":"git","url":"git://github.com/cowboy/javascript-sync-async-foreach.git"},"versions":{"0.1.3":{"author":{"name":"\"Cowboy\" Ben Alman","url":"http://benalman.com/"},"name":"async-foreach","description":"An optionally-asynchronous forEach with an interesting interface.","version":"0.1.3","homepage":"http://github.com/cowboy/javascript-sync-async-foreach","bugs":"https://github.com/cowboy/javascript-sync-async-foreach/issues","repository":{"type":"git","url":"git://github.com/cowboy/javascript-sync-async-foreach.git"},"main":"lib/foreach","engines":{"node":"*"},"keywords":["array","loop","sync","async","foreach"],"dependencies":{},"devDependencies":{},"readmeFilename":"README.md","_id":"async-foreach@0.1.3","dist":{"shasum":"36121f845c0578172de419a97dbeb1d16ec34542","size":4627,"noattachment":false,"key":"/async-foreach/-/async-foreach-0.1.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/async-foreach/download/async-foreach-0.1.3.tgz"},"_npmVersion":"1.1.70","_npmUser":{"name":"cowboy","email":"cowboy@rj3.net"},"maintainers":[{"name":"cowboy","email":""}],"directories":{},"publish_time":1367240146176,"_hasShrinkwrap":false,"_cnpm_publish_time":1367240146176},"0.1.2":{"author":{"name":"\"Cowboy\" Ben Alman","url":"http://benalman.com/"},"name":"async-foreach","description":"An optionally-asynchronous forEach with an interesting interface.","version":"0.1.2","homepage":"http://github.com/cowboy/javascript-sync-async-foreach","bugs":{"name":"https://github.com/cowboy/javascript-sync-async-foreach/issues"},"repository":{"type":"git","url":"git://github.com/cowboy/javascript-sync-async-foreach.git"},"main":"lib/foreach","engines":{"node":"~0.6.6"},"keywords":["array","loop","sync","async","foreach"],"dependencies":{},"devDependencies":{},"_npmUser":{"name":"cowboy","email":"cowboy@rj3.net"},"_id":"async-foreach@0.1.2","_engineSupported":true,"_npmVersion":"1.0.106","_nodeVersion":"v0.6.6","_defaultsLoaded":true,"dist":{"shasum":"c622ef609fca9af9be1a6f9e7fdc57be32be8035","size":10240,"noattachment":false,"key":"/async-foreach/-/async-foreach-0.1.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/async-foreach/download/async-foreach-0.1.2.tgz"},"maintainers":[{"name":"cowboy","email":""}],"directories":{},"publish_time":1326250491611,"_hasShrinkwrap":false,"_cnpm_publish_time":1326250491611}},"readme":"# JavaScript Sync/Async forEach\n\nAn optionally-asynchronous forEach with an interesting interface.\n\n## Getting Started\n\nThis code should work just fine in Node.js:\n\nFirst, install the module with: `npm install async-foreach`\n\n```javascript\nvar forEach = require('async-foreach').forEach;\nforEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n});\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// each c 2 [\"a\", \"b\", \"c\"]\n```\n\nOr in the browser:\n\n```html\n<script src=\"dist/ba-foreach.min.js\"></script>\n<script>\nforEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n});\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// each c 2 [\"a\", \"b\", \"c\"]\n</script>\n```\n\nIn the browser, you can attach the forEach method to any object.\n\n```html\n<script>\nthis.exports = Bocoup.utils;\n</script>\n<script src=\"dist/ba-foreach.min.js\"></script>\n<script>\nBocoup.utils.forEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n});\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// each c 2 [\"a\", \"b\", \"c\"]\n</script>\n```\n\n## The General Idea (Why I thought this was worth sharing)\n\nThe idea is to allow the callback to decide _at runtime_ whether the loop will be synchronous or asynchronous. By using `this` in a creative way (in situations where that value isn't already spoken for), an entire control API can be offered without over-complicating function signatures.\n\n```javascript\nforEach(arr, function(item, index) {\n  // Synchronous.\n});\n\nforEach(arr, function(item, index) {\n  // Only when `this.async` is called does iteration becomes asynchronous. The\n  // loop won't be continued until the `done` function is executed.\n  var done = this.async();\n  // Continue in one second.\n  setTimeout(done, 1000);\n});\n\nforEach(arr, function(item, index) {\n  // Break out of synchronous iteration early by returning false.\n  return index !== 1;\n});\n\nforEach(arr, function(item, index) {\n  // Break out of asynchronous iteration early...\n  var done = this.async();\n  // ...by passing false to the done function.\n  setTimeout(function() {\n    done(index !== 1);\n  });\n});\n```\n\n## Examples\nSee the unit tests for more examples.\n\n```javascript\n// Generic \"done\" callback.\nfunction allDone(notAborted, arr) {\n  console.log(\"done\", notAborted, arr);\n}\n\n// Synchronous.\nforEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n}, allDone);\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// each c 2 [\"a\", \"b\", \"c\"]\n// done true [\"a\", \"b\", \"c\"]\n\n// Synchronous with early abort.\nforEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n  if (item === \"b\") { return false; }\n}, allDone);\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// done false [\"a\", \"b\", \"c\"]\n\n// Asynchronous.\nforEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n  var done = this.async();\n  setTimeout(function() {\n    done();\n  }, 500);\n}, allDone);\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// each c 2 [\"a\", \"b\", \"c\"]\n// done true [\"a\", \"b\", \"c\"]\n\n// Asynchronous with early abort.\nforEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n  var done = this.async();\n  setTimeout(function() {\n    done(item !== \"b\");\n  }, 500);\n}, allDone);\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// done false [\"a\", \"b\", \"c\"]\n\n// Not actually asynchronous.\nforEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n  var done = this.async()\n  done();\n}, allDone);\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// each c 2 [\"a\", \"b\", \"c\"]\n// done true [\"a\", \"b\", \"c\"]\n\n// Not actually asynchronous with early abort.\nforEach([\"a\", \"b\", \"c\"], function(item, index, arr) {\n  console.log(\"each\", item, index, arr);\n  var done = this.async();\n  done(item !== \"b\");\n}, allDone);\n// logs:\n// each a 0 [\"a\", \"b\", \"c\"]\n// each b 1 [\"a\", \"b\", \"c\"]\n// done false [\"a\", \"b\", \"c\"]\n```\n\n## Contributing\nIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/cowboy/grunt).\n\n_Also, please don't edit files in the \"dist\" subdirectory as they are generated via grunt. You'll find source code in the \"lib\" subdirectory!_\n\n## Release History\n\n11/11/2011\nv0.1.0\nInitial Release.\n\n11/11/2011\nv0.1.1\nRefactored code to be much simpler. Yay for unit tests!\n\n11/17/2011\nv0.1.2\nAdding sparse array support.\nInvalid length properties are now sanitized.\nThis closes issue #1 (like a boss).\n\n## License\nCopyright (c) 2012 \"Cowboy\" Ben Alman  \nLicensed under the MIT license.  \n<http://benalman.com/about/license/>\n","_attachments":{},"readmeFilename":"README.md","homepage":"http://github.com/cowboy/javascript-sync-async-foreach","bugs":"https://github.com/cowboy/javascript-sync-async-foreach/issues"}