freezerwatch 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/freezerwatch +9 -0
- data/bin/setup +7 -0
- data/freezerwatch.gemspec +23 -0
- data/lib/freezerwatch.rb +5 -0
- data/lib/freezerwatch/version.rb +3 -0
- data/node_modules/.bin/freezerwatch +139 -0
- data/node_modules/freezerwatch/.npmignore +3 -0
- data/node_modules/freezerwatch/.ruby-version +1 -0
- data/node_modules/freezerwatch/Gemfile +4 -0
- data/node_modules/freezerwatch/Gemfile.lock +79 -0
- data/node_modules/freezerwatch/LICENSE +23 -0
- data/node_modules/freezerwatch/README.md +41 -0
- data/node_modules/freezerwatch/Rakefile +13 -0
- data/node_modules/freezerwatch/freezerwatch.js +139 -0
- data/node_modules/freezerwatch/metrics/bigfiles_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/cane_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/flay_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/flog_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/punchlist_high_water_mark +2 -0
- data/node_modules/freezerwatch/metrics/reek_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/rubocop_high_water_mark +1 -0
- data/node_modules/freezerwatch/node_modules/async/.jshintrc +25 -0
- data/node_modules/freezerwatch/node_modules/async/.travis.yml +6 -0
- data/node_modules/freezerwatch/node_modules/async/CHANGELOG.md +65 -0
- data/node_modules/freezerwatch/node_modules/async/LICENSE +19 -0
- data/node_modules/freezerwatch/node_modules/async/README.md +1803 -0
- data/node_modules/freezerwatch/node_modules/async/bower.json +43 -0
- data/node_modules/freezerwatch/node_modules/async/component.json +17 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/base.css +182 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/index.html +73 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/base.css +182 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/index.html +73 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/lib/async.js.html +3900 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/lib/index.html +73 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/prettify.css +1 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/prettify.js +1 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/sorter.js +156 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov.info +1452 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lib/async.js.html +3597 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lib/index.html +73 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/prettify.css +1 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/prettify.js +1 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/sort-arrow-sprite.png +0 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/sorter.js +156 -0
- data/node_modules/freezerwatch/node_modules/async/lib/async.js +1191 -0
- data/node_modules/freezerwatch/node_modules/async/nyc_output/5074.json +1 -0
- data/node_modules/freezerwatch/node_modules/async/package.json +96 -0
- data/node_modules/freezerwatch/node_modules/async/support/sync-package-managers.js +53 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/.npmignore +15 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/Client.js +60 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/Device.js +38 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/DeviceRawReadStream.js +36 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/DeviceSingleRawReadStream.js +29 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/DeviceStream.js +35 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/LICENSE +23 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/README.md +25 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/.npmignore +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/.travis.yml +12 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/README.md +364 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/index.js +156 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/lib/copy.js +8 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/lib/debug.js +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/lib/getSafe.js +34 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/lib/optional.js +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/.bin/uuid +26 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/aws-sign2/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/aws-sign2/README.md +4 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/aws-sign2/index.js +202 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/aws-sign2/package.json +46 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/forever-agent/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/forever-agent/README.md +4 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/forever-agent/index.js +119 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/forever-agent/package.json +45 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/License +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/Readme.md +175 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/lib/form_data.js +351 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/LICENSE +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/README.md +1647 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/bower.json +38 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/component.json +16 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/lib/async.js +1123 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/package.json +84 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/support/sync-package-managers.js +53 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/License +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/Readme.md +132 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/lib/combined_stream.js +188 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/.npmignore +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/License +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/Makefile +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/Readme.md +154 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js +99 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/package.json +42 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/common.js +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-delayed-http-upload.js +38 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-delayed-stream-auto-pause.js +21 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-delayed-stream-pause.js +14 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-delayed-stream.js +48 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-handle-source-errors.js +15 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-max-data-size.js +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-pipe-resumes.js +13 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-proxy-readable.js +13 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/run.js +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/package.json +60 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/package.json +80 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/Makefile +10 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/README.md +627 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/example/usage.js +78 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/images/hawk.png +0 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/images/logo.png +0 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/browser.js +485 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/client.js +367 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/crypto.js +111 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/index.js +15 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/server.js +524 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/utils.js +183 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/Makefile +11 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/README.md +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/images/boom.png +0 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/lib/index.js +207 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/package.json +64 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/test/index.js +245 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/Makefile +11 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/README.md +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js +68 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/package.json +65 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/test/index.js +101 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/LICENSE +33 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/Makefile +10 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/README.md +436 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/images/hoek.png +0 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/lib/escape.js +132 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/lib/index.js +585 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/package.json +70 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/escaper.js +86 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/index.js +1078 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/modules/test1.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/modules/test2.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/modules/test3.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/Makefile +11 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/README.md +68 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/examples/offset.js +16 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/examples/time.js +25 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/lib/index.js +409 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/package.json +65 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/test/index.js +359 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/package.json +70 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/browser.js +771 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/client.js +206 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/crypto.js +86 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/index.js +316 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/message.js +246 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/readme.js +98 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/server.js +686 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/uri.js +456 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/utils.js +120 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/.dir-locals.el +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/.npmignore +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/LICENSE +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/README.md +79 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/http_signing.md +296 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/index.js +26 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/parser.js +304 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/signer.js +178 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/util.js +304 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/verify.js +42 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/.npmignore +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/LICENSE +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/README.md +50 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/errors.js +13 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/index.js +27 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/reader.js +267 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/types.js +36 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/writer.js +317 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/index.js +20 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/package.json +63 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/tst/ber/reader.test.js +172 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/tst/ber/writer.test.js +296 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/assert-plus/README.md +126 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/assert-plus/assert.js +245 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/assert-plus/package.json +45 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/.npmignore +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/CHANGELOG +78 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/README +82 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/README.old +298 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/ctf.js +245 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/ctio.js +1485 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/ctype.js +944 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/man/man3ctype/ctio.3ctype +241 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/package.json +42 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/tools/jsl.conf +129 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/tools/jsstyle +839 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/package.json +72 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/.npmignore +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/CHANGELOG.md +14 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/LICENSE +15 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/Makefile +35 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/README.md +52 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/package.json +68 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/stringify.js +27 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/test/mocha.opts +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/test/stringify_test.js +246 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/LICENSE +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/README.md +66 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/mime.js +114 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/package.json +57 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/test.js +84 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/types/mime.types +1588 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/types/node.types +77 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/.npmignore +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/LICENSE.md +21 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/README.md +243 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/README.md +53 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/bench.gnu +174 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/bench.sh +34 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/benchmark-native.c +34 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/benchmark.js +84 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/bin/uuid +26 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/bower.json +23 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/component.json +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/package.json +65 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/test/compare_v1.js +63 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/test/test.html +17 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/test/test.js +228 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/uuid.js +247 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/README.md +4 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/index.js +43 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/package.json +48 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/test.js +49 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/.gitmodules +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/.npmignore +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/Readme.md +58 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/index.js +366 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/package.json +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/.jshintrc +72 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/.npmignore +3 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/LICENSE +78 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/README.md +380 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/generate-pubsuffix.js +230 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/lib/cookie.js +947 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/lib/memstore.js +102 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/lib/pubsuffix.js +69 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/node_modules/punycode/LICENSE-MIT.txt +20 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/node_modules/punycode/README.md +176 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/node_modules/punycode/package.json +81 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/node_modules/punycode/punycode.js +530 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/package.json +63 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/public-suffix.txt +5229 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/test.js +1340 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tunnel-agent/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tunnel-agent/README.md +4 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tunnel-agent/index.js +227 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tunnel-agent/package.json +45 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/package.json +74 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/request.js +1244 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/package.json +34 -0
- data/node_modules/freezerwatch/node_modules/optparse/README.md +163 -0
- data/node_modules/freezerwatch/node_modules/optparse/TODO +1 -0
- data/node_modules/freezerwatch/node_modules/optparse/examples/browser-test.html +75 -0
- data/node_modules/freezerwatch/node_modules/optparse/examples/nodejs-test.js +86 -0
- data/node_modules/freezerwatch/node_modules/optparse/lib/optparse.js +309 -0
- data/node_modules/freezerwatch/node_modules/optparse/package.json +46 -0
- data/node_modules/freezerwatch/node_modules/optparse/seed.yml +5 -0
- data/node_modules/freezerwatch/node_modules/osenv/.npmignore +13 -0
- data/node_modules/freezerwatch/node_modules/osenv/.travis.yml +9 -0
- data/node_modules/freezerwatch/node_modules/osenv/LICENSE +15 -0
- data/node_modules/freezerwatch/node_modules/osenv/README.md +63 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-homedir/index.js +24 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-homedir/license +21 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-homedir/package.json +70 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-homedir/readme.md +33 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-tmpdir/index.js +25 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-tmpdir/license +21 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-tmpdir/package.json +70 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-tmpdir/readme.md +36 -0
- data/node_modules/freezerwatch/node_modules/osenv/osenv.js +72 -0
- data/node_modules/freezerwatch/node_modules/osenv/package.json +76 -0
- data/node_modules/freezerwatch/node_modules/osenv/test/unix.js +71 -0
- data/node_modules/freezerwatch/node_modules/osenv/test/windows.js +74 -0
- data/node_modules/freezerwatch/node_modules/osenv/x.tap +39 -0
- data/node_modules/freezerwatch/package.json +37 -0
- metadata +384 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'quality/rake/task'
|
2
|
+
|
3
|
+
Quality::Rake::Task.new do |t|
|
4
|
+
t.output_dir = 'metrics'
|
5
|
+
t.verbose = true
|
6
|
+
# XXX: Need to make quality defaults and config work better for JS.
|
7
|
+
#
|
8
|
+
# extra_files are assumed to be Ruby files, for starters
|
9
|
+
t.extra_files = ['*.js', 'Rakefile']
|
10
|
+
t.skip_tools = %w(rubocop reek flog)
|
11
|
+
end
|
12
|
+
|
13
|
+
task default: :quality
|
@@ -0,0 +1,139 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
var lacrosse = require("lacrosse");
|
4
|
+
|
5
|
+
var fs = require('fs');
|
6
|
+
|
7
|
+
var osenv = require('osenv');
|
8
|
+
|
9
|
+
var homedir = osenv.home();
|
10
|
+
|
11
|
+
var config = JSON.parse(fs.readFileSync(homedir + '/private/freezerwatch.json', 'utf8'));
|
12
|
+
|
13
|
+
var client = new lacrosse.Client(config);
|
14
|
+
|
15
|
+
var optparse = require('optparse');
|
16
|
+
|
17
|
+
var async = require('async');
|
18
|
+
|
19
|
+
var verbose = false;
|
20
|
+
|
21
|
+
function debug(message) {
|
22
|
+
if (verbose) {
|
23
|
+
console.log(message);
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
function usage(usageString, code) {
|
28
|
+
console.log(usageString.toString());
|
29
|
+
return process.exit(code);
|
30
|
+
}
|
31
|
+
|
32
|
+
function parseDeviceIds() {
|
33
|
+
var deviceIds = [];
|
34
|
+
var mode;
|
35
|
+
var switches = [
|
36
|
+
['-h', '--help', 'Shows help sections'],
|
37
|
+
['-l', '--live', 'Report on liveness of the sensor system. Returns 0 exit code if all sensors are reading within the last day and have full batteries.'],
|
38
|
+
['-v', '--verbose', 'Report debugging information.'],
|
39
|
+
['-d STRING', '--device STRING', "Which device to monitor--specify this argument multiple times to monitor multiple devices. You can find device IDs by logging into lacrossealerts.com/login and looking at the link that your 'Download' button points to."],
|
40
|
+
];
|
41
|
+
|
42
|
+
// Create a new OptionParser.
|
43
|
+
var parser = new optparse.OptionParser(switches);
|
44
|
+
|
45
|
+
parser.banner = "Usage: freezerwatch --live --device=\"123\" --device=\"456\" --device=\"789\"";
|
46
|
+
|
47
|
+
var help = parser.toString();
|
48
|
+
|
49
|
+
// Hook the help option. The callback will be executed when the OptionParser
|
50
|
+
// hits the switch `-h` or `--help`.
|
51
|
+
parser.on('help', function() {
|
52
|
+
usage(help, 0);
|
53
|
+
});
|
54
|
+
|
55
|
+
parser.on('live', function() {
|
56
|
+
mode = 'live';
|
57
|
+
});
|
58
|
+
|
59
|
+
parser.on('verbose', function() {
|
60
|
+
verbose = true;
|
61
|
+
});
|
62
|
+
|
63
|
+
parser.on('device', function(name, deviceId) {
|
64
|
+
debug("Parsed device Id " + deviceId);
|
65
|
+
deviceIds.push(deviceId);
|
66
|
+
});
|
67
|
+
|
68
|
+
parser.parse(process.argv);
|
69
|
+
|
70
|
+
return { help: help, mode: mode, deviceIds: deviceIds};
|
71
|
+
}
|
72
|
+
|
73
|
+
var options = parseDeviceIds(process.argv);
|
74
|
+
|
75
|
+
if (!options.mode) {
|
76
|
+
usage(options.help, 1);
|
77
|
+
}
|
78
|
+
|
79
|
+
function toDate(str) {
|
80
|
+
var d = new Date(str);
|
81
|
+
console.log("Out of string " + str + ", parsed out " + d);
|
82
|
+
return d;
|
83
|
+
}
|
84
|
+
|
85
|
+
function yesterday() {
|
86
|
+
var d = new Date();
|
87
|
+
d.setDate(d.getDate() - 1);
|
88
|
+
return d;
|
89
|
+
}
|
90
|
+
|
91
|
+
function isLive(reading) {
|
92
|
+
debug("parsing this reading: " + JSON.stringify(reading));
|
93
|
+
return new Date(reading.timestamp) > yesterday() &&
|
94
|
+
!reading.lowBattery;
|
95
|
+
}
|
96
|
+
|
97
|
+
async.map(options.deviceIds,
|
98
|
+
function(deviceId, cb) {
|
99
|
+
debug("Pulling data for " + deviceId);
|
100
|
+
var device = new client.Device(deviceId);
|
101
|
+
debug("created device " + deviceId);
|
102
|
+
var s = device.createSingleReadStream();
|
103
|
+
debug("created stream");
|
104
|
+
var called = false;
|
105
|
+
s.on("data", function(data) {
|
106
|
+
if (!called) {
|
107
|
+
cb(null, data);
|
108
|
+
}
|
109
|
+
called = true;});
|
110
|
+
s.on("error", function(error) { cb(error, nil); });
|
111
|
+
//stream.on("error", console.log);
|
112
|
+
debug("events registered");
|
113
|
+
},
|
114
|
+
function(err, result) {
|
115
|
+
if (err) {
|
116
|
+
debug("error is " + JSON.stringify(err));
|
117
|
+
throw err;
|
118
|
+
} else {
|
119
|
+
debug("result is " + JSON.stringify(result));
|
120
|
+
everythingIsLive = result.map(isLive).reduce(function(everythingElseLive, thisItemLive) {
|
121
|
+
return everythingElseLive && thisItemLive;
|
122
|
+
});
|
123
|
+
if (everythingIsLive) {
|
124
|
+
process.exit(0);
|
125
|
+
} else {
|
126
|
+
process.exit(1);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
});
|
130
|
+
|
131
|
+
// XXX: add JSHint to rake quality
|
132
|
+
|
133
|
+
// XXX: Brainstorm errors to handle, add (manual) tests
|
134
|
+
|
135
|
+
// XXX: Crib style hints from optparse
|
136
|
+
|
137
|
+
// XXX: get gulp-file with node-quality to replace Rakefile with Quality
|
138
|
+
|
139
|
+
// XXX: Figure out style help tool
|
@@ -0,0 +1 @@
|
|
1
|
+
43
|
@@ -0,0 +1 @@
|
|
1
|
+
0
|
@@ -0,0 +1 @@
|
|
1
|
+
0
|
@@ -0,0 +1 @@
|
|
1
|
+
0
|
@@ -0,0 +1 @@
|
|
1
|
+
0
|
@@ -0,0 +1 @@
|
|
1
|
+
0
|
@@ -0,0 +1,25 @@
|
|
1
|
+
{
|
2
|
+
// Enforcing options
|
3
|
+
"eqeqeq": false,
|
4
|
+
"forin": true,
|
5
|
+
"indent": 4,
|
6
|
+
"noarg": true,
|
7
|
+
"undef": true,
|
8
|
+
"unused": true,
|
9
|
+
"trailing": true,
|
10
|
+
"evil": true,
|
11
|
+
"laxcomma": true,
|
12
|
+
|
13
|
+
// Relaxing options
|
14
|
+
"onevar": false,
|
15
|
+
"asi": false,
|
16
|
+
"eqnull": true,
|
17
|
+
"expr": false,
|
18
|
+
"loopfunc": true,
|
19
|
+
"sub": true,
|
20
|
+
"browser": true,
|
21
|
+
"node": true,
|
22
|
+
"globals": {
|
23
|
+
"define": true
|
24
|
+
}
|
25
|
+
}
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# v1.2.1
|
2
|
+
|
3
|
+
Bug Fix:
|
4
|
+
|
5
|
+
- Small regression with synchronous iterator behavior in `eachSeries` with a 1-element array. Before 1.1.0, `eachSeries`'s callback was called on the same tick, which this patch restores. In 2.0.0, it will be called on the next tick. (#782)
|
6
|
+
|
7
|
+
# v1.2.0
|
8
|
+
|
9
|
+
New Features:
|
10
|
+
|
11
|
+
- Added `timesLimit` (#743)
|
12
|
+
- `concurrency` can be changed after initialization in `queue` by setting `q.concurrency`. The new concurrency will be reflected the next time a task is processed. (#747, #772)
|
13
|
+
|
14
|
+
Bug Fixes:
|
15
|
+
|
16
|
+
- Fixed a regression in `each` and family with empty arrays that have additional properties. (#775, #777)
|
17
|
+
|
18
|
+
|
19
|
+
# v1.1.1
|
20
|
+
|
21
|
+
Bug Fix:
|
22
|
+
|
23
|
+
- Small regression with synchronous iterator behavior in `eachSeries` with a 1-element array. Before 1.1.0, `eachSeries`'s callback was called on the same tick, which this patch restores. In 2.0.0, it will be called on the next tick. (#782)
|
24
|
+
|
25
|
+
|
26
|
+
# v1.1.0
|
27
|
+
|
28
|
+
New Features:
|
29
|
+
|
30
|
+
- `cargo` now supports all of the same methods and event callbacks as `queue`.
|
31
|
+
- Added `ensureAsync` - A wrapper that ensures an async function calls its callback on a later tick. (#769)
|
32
|
+
- Optimized `map`, `eachOf`, and `waterfall` families of functions
|
33
|
+
- Passing a `null` or `undefined` array to `map`, `each`, `parallel` and families will be treated as an empty array (#667).
|
34
|
+
- The callback is now optional for the composed results of `compose` and `seq`. (#618)
|
35
|
+
- Reduced file size by 4kb, (minified version by 1kb)
|
36
|
+
- Added code coverage through `nyc` and `coveralls` (#768)
|
37
|
+
|
38
|
+
Bug Fixes:
|
39
|
+
|
40
|
+
- `forever` will no longer stack overflow with a synchronous iterator (#622)
|
41
|
+
- `eachLimit` and other limit functions will stop iterating once an error occurs (#754)
|
42
|
+
- Always pass `null` in callbacks when there is no error (#439)
|
43
|
+
- Ensure proper conditions when calling `drain()` after pushing an empty data set to a queue (#668)
|
44
|
+
- `each` and family will properly handle an empty array (#578)
|
45
|
+
- `eachSeries` and family will finish if the underlying array is modified during execution (#557)
|
46
|
+
- `queue` will throw if a non-function is passed to `q.push()` (#593)
|
47
|
+
- Doc fixes (#629, #766)
|
48
|
+
|
49
|
+
|
50
|
+
# v1.0.0
|
51
|
+
|
52
|
+
No known breaking changes, we are simply complying with semver from here on out.
|
53
|
+
|
54
|
+
Changes:
|
55
|
+
|
56
|
+
- Start using a changelog!
|
57
|
+
- Add `forEachOf` for iterating over Objects (or to iterate Arrays with indexes available) (#168 #704 #321)
|
58
|
+
- Detect deadlocks in `auto` (#663)
|
59
|
+
- Better support for require.js (#527)
|
60
|
+
- Throw if queue created with concurrency `0` (#714)
|
61
|
+
- Fix unneeded iteration in `queue.resume()` (#758)
|
62
|
+
- Guard against timer mocking overriding `setImmediate` (#609 #611)
|
63
|
+
- Miscellaneous doc fixes (#542 #596 #615 #628 #631 #690 #729)
|
64
|
+
- Use single noop function internally (#546)
|
65
|
+
- Optimize internal `_each`, `_map` and `_keys` functions.
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010-2014 Caolan McMahon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
@@ -0,0 +1,1803 @@
|
|
1
|
+
# Async.js
|
2
|
+
|
3
|
+
[![Build Status via Travis CI](https://travis-ci.org/caolan/async.svg?branch=master)](https://travis-ci.org/caolan/async)
|
4
|
+
[![NPM version](http://img.shields.io/npm/v/async.svg)](https://www.npmjs.org/package/async)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/caolan/async/badge.svg?branch=master)](https://coveralls.io/r/caolan/async?branch=master)
|
6
|
+
|
7
|
+
Async is a utility module which provides straight-forward, powerful functions
|
8
|
+
for working with asynchronous JavaScript. Although originally designed for
|
9
|
+
use with [Node.js](http://nodejs.org) and installable via `npm install async`,
|
10
|
+
it can also be used directly in the browser.
|
11
|
+
|
12
|
+
Async is also installable via:
|
13
|
+
|
14
|
+
- [bower](http://bower.io/): `bower install async`
|
15
|
+
- [component](https://github.com/component/component): `component install
|
16
|
+
caolan/async`
|
17
|
+
- [jam](http://jamjs.org/): `jam install async`
|
18
|
+
- [spm](http://spmjs.io/): `spm install async`
|
19
|
+
|
20
|
+
Async provides around 20 functions that include the usual 'functional'
|
21
|
+
suspects (`map`, `reduce`, `filter`, `each`…) as well as some common patterns
|
22
|
+
for asynchronous control flow (`parallel`, `series`, `waterfall`…). All these
|
23
|
+
functions assume you follow the Node.js convention of providing a single
|
24
|
+
callback as the last argument of your `async` function.
|
25
|
+
|
26
|
+
|
27
|
+
## Quick Examples
|
28
|
+
|
29
|
+
```javascript
|
30
|
+
async.map(['file1','file2','file3'], fs.stat, function(err, results){
|
31
|
+
// results is now an array of stats for each file
|
32
|
+
});
|
33
|
+
|
34
|
+
async.filter(['file1','file2','file3'], fs.exists, function(results){
|
35
|
+
// results now equals an array of the existing files
|
36
|
+
});
|
37
|
+
|
38
|
+
async.parallel([
|
39
|
+
function(){ ... },
|
40
|
+
function(){ ... }
|
41
|
+
], callback);
|
42
|
+
|
43
|
+
async.series([
|
44
|
+
function(){ ... },
|
45
|
+
function(){ ... }
|
46
|
+
]);
|
47
|
+
```
|
48
|
+
|
49
|
+
There are many more functions available so take a look at the docs below for a
|
50
|
+
full list. This module aims to be comprehensive, so if you feel anything is
|
51
|
+
missing please create a GitHub issue for it.
|
52
|
+
|
53
|
+
## Common Pitfalls
|
54
|
+
|
55
|
+
<a name="stack-overflow">
|
56
|
+
### Synchronous iteration functions
|
57
|
+
|
58
|
+
If you get an error like `RangeError: Maximum call stack size exceeded.` or other stack overflow issues when using async, you are likely using a synchronous iterator. By *synchronous* we mean a function that calls its callback on the same tick in the javascript event loop, without doing any I/O or using any timers. Calling many callbacks iteratively will quickly overflow the stack. If you run into this issue, just defer your callback with `async.nextTick` to start a new call stack on the next tick of the event loop.
|
59
|
+
|
60
|
+
This can also arise by accident if you callback early in certain cases:
|
61
|
+
|
62
|
+
```js
|
63
|
+
async.eachSeries(hugeArray, function iterator(item, callback) {
|
64
|
+
if (inCache(item)) {
|
65
|
+
callback(null, cache[item]); // if many items are cached, you'll overflow
|
66
|
+
} else {
|
67
|
+
doSomeIO(item, callback);
|
68
|
+
}
|
69
|
+
}, function done() {
|
70
|
+
//...
|
71
|
+
});
|
72
|
+
```
|
73
|
+
|
74
|
+
Just change it to:
|
75
|
+
|
76
|
+
```js
|
77
|
+
async.eachSeries(hugeArray, function iterator(item, callback) {
|
78
|
+
if (inCache(item)) {
|
79
|
+
async.setImmediate(function () {
|
80
|
+
callback(null, cache[item]);
|
81
|
+
});
|
82
|
+
} else {
|
83
|
+
doSomeIO(item, callback);
|
84
|
+
//...
|
85
|
+
```
|
86
|
+
|
87
|
+
Async guards against synchronous functions in some, but not all, cases. If you are still running into stack overflows, you can defer as suggested above, or wrap functions with [`async.ensureAsync`](#ensureAsync) Functions that are asynchronous by their nature do not have this problem and don't need the extra callback deferral.
|
88
|
+
|
89
|
+
If javascript's event loop is still a bit nebulous, check out [this article](http://blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/) or [this talk](http://2014.jsconf.eu/speakers/philip-roberts-what-the-heck-is-the-event-loop-anyway.html) for more detailed information about how it works.
|
90
|
+
|
91
|
+
|
92
|
+
### Binding a context to an iterator
|
93
|
+
|
94
|
+
This section is really about `bind`, not about `async`. If you are wondering how to
|
95
|
+
make `async` execute your iterators in a given context, or are confused as to why
|
96
|
+
a method of another library isn't working as an iterator, study this example:
|
97
|
+
|
98
|
+
```js
|
99
|
+
// Here is a simple object with an (unnecessarily roundabout) squaring method
|
100
|
+
var AsyncSquaringLibrary = {
|
101
|
+
squareExponent: 2,
|
102
|
+
square: function(number, callback){
|
103
|
+
var result = Math.pow(number, this.squareExponent);
|
104
|
+
setTimeout(function(){
|
105
|
+
callback(null, result);
|
106
|
+
}, 200);
|
107
|
+
}
|
108
|
+
};
|
109
|
+
|
110
|
+
async.map([1, 2, 3], AsyncSquaringLibrary.square, function(err, result){
|
111
|
+
// result is [NaN, NaN, NaN]
|
112
|
+
// This fails because the `this.squareExponent` expression in the square
|
113
|
+
// function is not evaluated in the context of AsyncSquaringLibrary, and is
|
114
|
+
// therefore undefined.
|
115
|
+
});
|
116
|
+
|
117
|
+
async.map([1, 2, 3], AsyncSquaringLibrary.square.bind(AsyncSquaringLibrary), function(err, result){
|
118
|
+
// result is [1, 4, 9]
|
119
|
+
// With the help of bind we can attach a context to the iterator before
|
120
|
+
// passing it to async. Now the square function will be executed in its
|
121
|
+
// 'home' AsyncSquaringLibrary context and the value of `this.squareExponent`
|
122
|
+
// will be as expected.
|
123
|
+
});
|
124
|
+
```
|
125
|
+
|
126
|
+
## Download
|
127
|
+
|
128
|
+
The source is available for download from
|
129
|
+
[GitHub](https://github.com/caolan/async/blob/master/lib/async.js).
|
130
|
+
Alternatively, you can install using Node Package Manager (`npm`):
|
131
|
+
|
132
|
+
npm install async
|
133
|
+
|
134
|
+
As well as using Bower:
|
135
|
+
|
136
|
+
bower install async
|
137
|
+
|
138
|
+
__Development:__ [async.js](https://github.com/caolan/async/raw/master/lib/async.js) - 29.6kb Uncompressed
|
139
|
+
|
140
|
+
## In the Browser
|
141
|
+
|
142
|
+
So far it's been tested in IE6, IE7, IE8, FF3.6 and Chrome 5.
|
143
|
+
|
144
|
+
Usage:
|
145
|
+
|
146
|
+
```html
|
147
|
+
<script type="text/javascript" src="async.js"></script>
|
148
|
+
<script type="text/javascript">
|
149
|
+
|
150
|
+
async.map(data, asyncProcess, function(err, results){
|
151
|
+
alert(results);
|
152
|
+
});
|
153
|
+
|
154
|
+
</script>
|
155
|
+
```
|
156
|
+
|
157
|
+
## Documentation
|
158
|
+
|
159
|
+
### Collections
|
160
|
+
|
161
|
+
* [`each`](#each)
|
162
|
+
* [`eachSeries`](#eachSeries)
|
163
|
+
* [`eachLimit`](#eachLimit)
|
164
|
+
* [`forEachOf`](#forEachOf)
|
165
|
+
* [`forEachOfSeries`](#forEachOfSeries)
|
166
|
+
* [`forEachOfLimit`](#forEachOfLimit)
|
167
|
+
* [`map`](#map)
|
168
|
+
* [`mapSeries`](#mapSeries)
|
169
|
+
* [`mapLimit`](#mapLimit)
|
170
|
+
* [`filter`](#filter)
|
171
|
+
* [`filterSeries`](#filterSeries)
|
172
|
+
* [`reject`](#reject)
|
173
|
+
* [`rejectSeries`](#rejectSeries)
|
174
|
+
* [`reduce`](#reduce)
|
175
|
+
* [`reduceRight`](#reduceRight)
|
176
|
+
* [`detect`](#detect)
|
177
|
+
* [`detectSeries`](#detectSeries)
|
178
|
+
* [`sortBy`](#sortBy)
|
179
|
+
* [`some`](#some)
|
180
|
+
* [`every`](#every)
|
181
|
+
* [`concat`](#concat)
|
182
|
+
* [`concatSeries`](#concatSeries)
|
183
|
+
|
184
|
+
### Control Flow
|
185
|
+
|
186
|
+
* [`series`](#seriestasks-callback)
|
187
|
+
* [`parallel`](#parallel)
|
188
|
+
* [`parallelLimit`](#parallellimittasks-limit-callback)
|
189
|
+
* [`whilst`](#whilst)
|
190
|
+
* [`doWhilst`](#doWhilst)
|
191
|
+
* [`until`](#until)
|
192
|
+
* [`doUntil`](#doUntil)
|
193
|
+
* [`forever`](#forever)
|
194
|
+
* [`waterfall`](#waterfall)
|
195
|
+
* [`compose`](#compose)
|
196
|
+
* [`seq`](#seq)
|
197
|
+
* [`applyEach`](#applyEach)
|
198
|
+
* [`applyEachSeries`](#applyEachSeries)
|
199
|
+
* [`queue`](#queue)
|
200
|
+
* [`priorityQueue`](#priorityQueue)
|
201
|
+
* [`cargo`](#cargo)
|
202
|
+
* [`auto`](#auto)
|
203
|
+
* [`retry`](#retry)
|
204
|
+
* [`iterator`](#iterator)
|
205
|
+
* [`apply`](#apply)
|
206
|
+
* [`nextTick`](#nextTick)
|
207
|
+
* [`times`](#times)
|
208
|
+
* [`timesSeries`](#timesSeries)
|
209
|
+
* [`timesLimit`](#timesLimit)
|
210
|
+
|
211
|
+
### Utils
|
212
|
+
|
213
|
+
* [`memoize`](#memoize)
|
214
|
+
* [`unmemoize`](#unmemoize)
|
215
|
+
* [`ensureAsync`](#ensureAsync)
|
216
|
+
* [`log`](#log)
|
217
|
+
* [`dir`](#dir)
|
218
|
+
* [`noConflict`](#noConflict)
|
219
|
+
|
220
|
+
|
221
|
+
## Collections
|
222
|
+
|
223
|
+
<a name="forEach" />
|
224
|
+
<a name="each" />
|
225
|
+
### each(arr, iterator, [callback])
|
226
|
+
|
227
|
+
Applies the function `iterator` to each item in `arr`, in parallel.
|
228
|
+
The `iterator` is called with an item from the list, and a callback for when it
|
229
|
+
has finished. If the `iterator` passes an error to its `callback`, the main
|
230
|
+
`callback` (for the `each` function) is immediately called with the error.
|
231
|
+
|
232
|
+
Note, that since this function applies `iterator` to each item in parallel,
|
233
|
+
there is no guarantee that the iterator functions will complete in order.
|
234
|
+
|
235
|
+
__Arguments__
|
236
|
+
|
237
|
+
* `arr` - An array to iterate over.
|
238
|
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
239
|
+
The iterator is passed a `callback(err)` which must be called once it has
|
240
|
+
completed. If no error has occurred, the `callback` should be run without
|
241
|
+
arguments or with an explicit `null` argument. The array index is not passed
|
242
|
+
to the iterator. If you need the index, use [`forEachOf`](#forEachOf).
|
243
|
+
* `callback(err)` - *Optional* A callback which is called when all `iterator` functions
|
244
|
+
have finished, or an error occurs.
|
245
|
+
|
246
|
+
__Examples__
|
247
|
+
|
248
|
+
|
249
|
+
```js
|
250
|
+
// assuming openFiles is an array of file names and saveFile is a function
|
251
|
+
// to save the modified contents of that file:
|
252
|
+
|
253
|
+
async.each(openFiles, saveFile, function(err){
|
254
|
+
// if any of the saves produced an error, err would equal that error
|
255
|
+
});
|
256
|
+
```
|
257
|
+
|
258
|
+
```js
|
259
|
+
// assuming openFiles is an array of file names
|
260
|
+
|
261
|
+
async.each(openFiles, function(file, callback) {
|
262
|
+
|
263
|
+
// Perform operation on file here.
|
264
|
+
console.log('Processing file ' + file);
|
265
|
+
|
266
|
+
if( file.length > 32 ) {
|
267
|
+
console.log('This file name is too long');
|
268
|
+
callback('File name too long');
|
269
|
+
} else {
|
270
|
+
// Do work to process file here
|
271
|
+
console.log('File processed');
|
272
|
+
callback();
|
273
|
+
}
|
274
|
+
}, function(err){
|
275
|
+
// if any of the file processing produced an error, err would equal that error
|
276
|
+
if( err ) {
|
277
|
+
// One of the iterations produced an error.
|
278
|
+
// All processing will now stop.
|
279
|
+
console.log('A file failed to process');
|
280
|
+
} else {
|
281
|
+
console.log('All files have been processed successfully');
|
282
|
+
}
|
283
|
+
});
|
284
|
+
```
|
285
|
+
|
286
|
+
---------------------------------------
|
287
|
+
|
288
|
+
<a name="forEachSeries" />
|
289
|
+
<a name="eachSeries" />
|
290
|
+
### eachSeries(arr, iterator, [callback])
|
291
|
+
|
292
|
+
The same as [`each`](#each), only `iterator` is applied to each item in `arr` in
|
293
|
+
series. The next `iterator` is only called once the current one has completed.
|
294
|
+
This means the `iterator` functions will complete in order.
|
295
|
+
|
296
|
+
|
297
|
+
---------------------------------------
|
298
|
+
|
299
|
+
<a name="forEachLimit" />
|
300
|
+
<a name="eachLimit" />
|
301
|
+
### eachLimit(arr, limit, iterator, [callback])
|
302
|
+
|
303
|
+
The same as [`each`](#each), only no more than `limit` `iterator`s will be simultaneously
|
304
|
+
running at any time.
|
305
|
+
|
306
|
+
Note that the items in `arr` are not processed in batches, so there is no guarantee that
|
307
|
+
the first `limit` `iterator` functions will complete before any others are started.
|
308
|
+
|
309
|
+
__Arguments__
|
310
|
+
|
311
|
+
* `arr` - An array to iterate over.
|
312
|
+
* `limit` - The maximum number of `iterator`s to run at any time.
|
313
|
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
314
|
+
The iterator is passed a `callback(err)` which must be called once it has
|
315
|
+
completed. If no error has occurred, the callback should be run without
|
316
|
+
arguments or with an explicit `null` argument.
|
317
|
+
* `callback(err)` - *Optional* A callback which is called when all `iterator` functions
|
318
|
+
have finished, or an error occurs.
|
319
|
+
|
320
|
+
__Example__
|
321
|
+
|
322
|
+
```js
|
323
|
+
// Assume documents is an array of JSON objects and requestApi is a
|
324
|
+
// function that interacts with a rate-limited REST api.
|
325
|
+
|
326
|
+
async.eachLimit(documents, 20, requestApi, function(err){
|
327
|
+
// if any of the saves produced an error, err would equal that error
|
328
|
+
});
|
329
|
+
```
|
330
|
+
|
331
|
+
---------------------------------------
|
332
|
+
|
333
|
+
<a name="forEachOf" />
|
334
|
+
<a name="eachOf" />
|
335
|
+
|
336
|
+
### forEachOf(obj, iterator, [callback])
|
337
|
+
|
338
|
+
Like `each`, except that it iterates over objects, and passes the key as the second argument to the iterator.
|
339
|
+
|
340
|
+
__Arguments__
|
341
|
+
|
342
|
+
* `obj` - An object or array to iterate over.
|
343
|
+
* `iterator(item, key, callback)` - A function to apply to each item in `obj`.
|
344
|
+
The `key` is the item's key, or index in the case of an array. The iterator is
|
345
|
+
passed a `callback(err)` which must be called once it has completed. If no
|
346
|
+
error has occurred, the callback should be run without arguments or with an
|
347
|
+
explicit `null` argument.
|
348
|
+
* `callback(err)` - *Optional* A callback which is called when all `iterator` functions have finished, or an error occurs.
|
349
|
+
|
350
|
+
__Example__
|
351
|
+
|
352
|
+
```js
|
353
|
+
var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
|
354
|
+
var configs = {};
|
355
|
+
|
356
|
+
async.forEachOf(obj, function (value, key, callback) {
|
357
|
+
fs.readFile(__dirname + value, "utf8", function (err, data) {
|
358
|
+
if (err) return callback(err);
|
359
|
+
try {
|
360
|
+
configs[key] = JSON.parse(data);
|
361
|
+
} catch (e) {
|
362
|
+
return callback(e);
|
363
|
+
}
|
364
|
+
callback();
|
365
|
+
})
|
366
|
+
}, function (err) {
|
367
|
+
if (err) console.error(err.message);
|
368
|
+
// configs is now a map of JSON data
|
369
|
+
doSomethingWith(configs);
|
370
|
+
})
|
371
|
+
```
|
372
|
+
|
373
|
+
---------------------------------------
|
374
|
+
|
375
|
+
<a name="forEachOfSeries" />
|
376
|
+
<a name="eachOfSeries" />
|
377
|
+
|
378
|
+
### forEachOfSeries(obj, iterator, [callback])
|
379
|
+
|
380
|
+
Like [`forEachOf`](#forEachOf), except only one `iterator` is run at a time. The order of execution is not guaranteed for objects, but it will be guaranteed for arrays.
|
381
|
+
|
382
|
+
---------------------------------------
|
383
|
+
|
384
|
+
<a name="forEachOfLimit" />
|
385
|
+
<a name="eachOfLimit" />
|
386
|
+
|
387
|
+
### forEachOfLimit(obj, limit, iterator, [callback])
|
388
|
+
|
389
|
+
Like [`forEachOf`](#forEachOf), except the number of `iterator`s running at a given time is controlled by `limit`.
|
390
|
+
|
391
|
+
|
392
|
+
---------------------------------------
|
393
|
+
|
394
|
+
<a name="map" />
|
395
|
+
### map(arr, iterator, [callback])
|
396
|
+
|
397
|
+
Produces a new array of values by mapping each value in `arr` through
|
398
|
+
the `iterator` function. The `iterator` is called with an item from `arr` and a
|
399
|
+
callback for when it has finished processing. Each of these callback takes 2 arguments:
|
400
|
+
an `error`, and the transformed item from `arr`. If `iterator` passes an error to its
|
401
|
+
callback, the main `callback` (for the `map` function) is immediately called with the error.
|
402
|
+
|
403
|
+
Note, that since this function applies the `iterator` to each item in parallel,
|
404
|
+
there is no guarantee that the `iterator` functions will complete in order.
|
405
|
+
However, the results array will be in the same order as the original `arr`.
|
406
|
+
|
407
|
+
__Arguments__
|
408
|
+
|
409
|
+
* `arr` - An array to iterate over.
|
410
|
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
411
|
+
The iterator is passed a `callback(err, transformed)` which must be called once
|
412
|
+
it has completed with an error (which can be `null`) and a transformed item.
|
413
|
+
* `callback(err, results)` - *Optional* A callback which is called when all `iterator`
|
414
|
+
functions have finished, or an error occurs. Results is an array of the
|
415
|
+
transformed items from the `arr`.
|
416
|
+
|
417
|
+
__Example__
|
418
|
+
|
419
|
+
```js
|
420
|
+
async.map(['file1','file2','file3'], fs.stat, function(err, results){
|
421
|
+
// results is now an array of stats for each file
|
422
|
+
});
|
423
|
+
```
|
424
|
+
|
425
|
+
---------------------------------------
|
426
|
+
|
427
|
+
<a name="mapSeries" />
|
428
|
+
### mapSeries(arr, iterator, [callback])
|
429
|
+
|
430
|
+
The same as [`map`](#map), only the `iterator` is applied to each item in `arr` in
|
431
|
+
series. The next `iterator` is only called once the current one has completed.
|
432
|
+
The results array will be in the same order as the original.
|
433
|
+
|
434
|
+
|
435
|
+
---------------------------------------
|
436
|
+
|
437
|
+
<a name="mapLimit" />
|
438
|
+
### mapLimit(arr, limit, iterator, [callback])
|
439
|
+
|
440
|
+
The same as [`map`](#map), only no more than `limit` `iterator`s will be simultaneously
|
441
|
+
running at any time.
|
442
|
+
|
443
|
+
Note that the items are not processed in batches, so there is no guarantee that
|
444
|
+
the first `limit` `iterator` functions will complete before any others are started.
|
445
|
+
|
446
|
+
__Arguments__
|
447
|
+
|
448
|
+
* `arr` - An array to iterate over.
|
449
|
+
* `limit` - The maximum number of `iterator`s to run at any time.
|
450
|
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
451
|
+
The iterator is passed a `callback(err, transformed)` which must be called once
|
452
|
+
it has completed with an error (which can be `null`) and a transformed item.
|
453
|
+
* `callback(err, results)` - A callback which is called when all `iterator`
|
454
|
+
calls have finished, or an error occurs. The result is an array of the
|
455
|
+
transformed items from the original `arr`.
|
456
|
+
|
457
|
+
__Example__
|
458
|
+
|
459
|
+
```js
|
460
|
+
async.mapLimit(['file1','file2','file3'], 1, fs.stat, function(err, results){
|
461
|
+
// results is now an array of stats for each file
|
462
|
+
});
|
463
|
+
```
|
464
|
+
|
465
|
+
---------------------------------------
|
466
|
+
|
467
|
+
<a name="select" />
|
468
|
+
<a name="filter" />
|
469
|
+
### filter(arr, iterator, [callback])
|
470
|
+
|
471
|
+
__Alias:__ `select`
|
472
|
+
|
473
|
+
Returns a new array of all the values in `arr` which pass an async truth test.
|
474
|
+
_The callback for each `iterator` call only accepts a single argument of `true` or
|
475
|
+
`false`; it does not accept an error argument first!_ This is in-line with the
|
476
|
+
way node libraries work with truth tests like `fs.exists`. This operation is
|
477
|
+
performed in parallel, but the results array will be in the same order as the
|
478
|
+
original.
|
479
|
+
|
480
|
+
__Arguments__
|
481
|
+
|
482
|
+
* `arr` - An array to iterate over.
|
483
|
+
* `iterator(item, callback)` - A truth test to apply to each item in `arr`.
|
484
|
+
The `iterator` is passed a `callback(truthValue)`, which must be called with a
|
485
|
+
boolean argument once it has completed.
|
486
|
+
* `callback(results)` - *Optional* A callback which is called after all the `iterator`
|
487
|
+
functions have finished.
|
488
|
+
|
489
|
+
__Example__
|
490
|
+
|
491
|
+
```js
|
492
|
+
async.filter(['file1','file2','file3'], fs.exists, function(results){
|
493
|
+
// results now equals an array of the existing files
|
494
|
+
});
|
495
|
+
```
|
496
|
+
|
497
|
+
---------------------------------------
|
498
|
+
|
499
|
+
<a name="selectSeries" />
|
500
|
+
<a name="filterSeries" />
|
501
|
+
### filterSeries(arr, iterator, [callback])
|
502
|
+
|
503
|
+
__Alias:__ `selectSeries`
|
504
|
+
|
505
|
+
The same as [`filter`](#filter) only the `iterator` is applied to each item in `arr` in
|
506
|
+
series. The next `iterator` is only called once the current one has completed.
|
507
|
+
The results array will be in the same order as the original.
|
508
|
+
|
509
|
+
---------------------------------------
|
510
|
+
|
511
|
+
<a name="reject" />
|
512
|
+
### reject(arr, iterator, [callback])
|
513
|
+
|
514
|
+
The opposite of [`filter`](#filter). Removes values that pass an `async` truth test.
|
515
|
+
|
516
|
+
---------------------------------------
|
517
|
+
|
518
|
+
<a name="rejectSeries" />
|
519
|
+
### rejectSeries(arr, iterator, [callback])
|
520
|
+
|
521
|
+
The same as [`reject`](#reject), only the `iterator` is applied to each item in `arr`
|
522
|
+
in series.
|
523
|
+
|
524
|
+
|
525
|
+
---------------------------------------
|
526
|
+
|
527
|
+
<a name="reduce" />
|
528
|
+
### reduce(arr, memo, iterator, [callback])
|
529
|
+
|
530
|
+
__Aliases:__ `inject`, `foldl`
|
531
|
+
|
532
|
+
Reduces `arr` into a single value using an async `iterator` to return
|
533
|
+
each successive step. `memo` is the initial state of the reduction.
|
534
|
+
This function only operates in series.
|
535
|
+
|
536
|
+
For performance reasons, it may make sense to split a call to this function into
|
537
|
+
a parallel map, and then use the normal `Array.prototype.reduce` on the results.
|
538
|
+
This function is for situations where each step in the reduction needs to be async;
|
539
|
+
if you can get the data before reducing it, then it's probably a good idea to do so.
|
540
|
+
|
541
|
+
__Arguments__
|
542
|
+
|
543
|
+
* `arr` - An array to iterate over.
|
544
|
+
* `memo` - The initial state of the reduction.
|
545
|
+
* `iterator(memo, item, callback)` - A function applied to each item in the
|
546
|
+
array to produce the next step in the reduction. The `iterator` is passed a
|
547
|
+
`callback(err, reduction)` which accepts an optional error as its first
|
548
|
+
argument, and the state of the reduction as the second. If an error is
|
549
|
+
passed to the callback, the reduction is stopped and the main `callback` is
|
550
|
+
immediately called with the error.
|
551
|
+
* `callback(err, result)` - *Optional* A callback which is called after all the `iterator`
|
552
|
+
functions have finished. Result is the reduced value.
|
553
|
+
|
554
|
+
__Example__
|
555
|
+
|
556
|
+
```js
|
557
|
+
async.reduce([1,2,3], 0, function(memo, item, callback){
|
558
|
+
// pointless async:
|
559
|
+
process.nextTick(function(){
|
560
|
+
callback(null, memo + item)
|
561
|
+
});
|
562
|
+
}, function(err, result){
|
563
|
+
// result is now equal to the last value of memo, which is 6
|
564
|
+
});
|
565
|
+
```
|
566
|
+
|
567
|
+
---------------------------------------
|
568
|
+
|
569
|
+
<a name="reduceRight" />
|
570
|
+
### reduceRight(arr, memo, iterator, [callback])
|
571
|
+
|
572
|
+
__Alias:__ `foldr`
|
573
|
+
|
574
|
+
Same as [`reduce`](#reduce), only operates on `arr` in reverse order.
|
575
|
+
|
576
|
+
|
577
|
+
---------------------------------------
|
578
|
+
|
579
|
+
<a name="detect" />
|
580
|
+
### detect(arr, iterator, [callback])
|
581
|
+
|
582
|
+
Returns the first value in `arr` that passes an async truth test. The
|
583
|
+
`iterator` is applied in parallel, meaning the first iterator to return `true` will
|
584
|
+
fire the detect `callback` with that result. That means the result might not be
|
585
|
+
the first item in the original `arr` (in terms of order) that passes the test.
|
586
|
+
|
587
|
+
If order within the original `arr` is important, then look at [`detectSeries`](#detectSeries).
|
588
|
+
|
589
|
+
__Arguments__
|
590
|
+
|
591
|
+
* `arr` - An array to iterate over.
|
592
|
+
* `iterator(item, callback)` - A truth test to apply to each item in `arr`.
|
593
|
+
The iterator is passed a `callback(truthValue)` which must be called with a
|
594
|
+
boolean argument once it has completed. **Note: this callback does not take an error as its first argument.**
|
595
|
+
* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns
|
596
|
+
`true`, or after all the `iterator` functions have finished. Result will be
|
597
|
+
the first item in the array that passes the truth test (iterator) or the
|
598
|
+
value `undefined` if none passed. **Note: this callback does not take an error as its first argument.**
|
599
|
+
|
600
|
+
__Example__
|
601
|
+
|
602
|
+
```js
|
603
|
+
async.detect(['file1','file2','file3'], fs.exists, function(result){
|
604
|
+
// result now equals the first file in the list that exists
|
605
|
+
});
|
606
|
+
```
|
607
|
+
|
608
|
+
---------------------------------------
|
609
|
+
|
610
|
+
<a name="detectSeries" />
|
611
|
+
### detectSeries(arr, iterator, [callback])
|
612
|
+
|
613
|
+
The same as [`detect`](#detect), only the `iterator` is applied to each item in `arr`
|
614
|
+
in series. This means the result is always the first in the original `arr` (in
|
615
|
+
terms of array order) that passes the truth test.
|
616
|
+
|
617
|
+
|
618
|
+
---------------------------------------
|
619
|
+
|
620
|
+
<a name="sortBy" />
|
621
|
+
### sortBy(arr, iterator, [callback])
|
622
|
+
|
623
|
+
Sorts a list by the results of running each `arr` value through an async `iterator`.
|
624
|
+
|
625
|
+
__Arguments__
|
626
|
+
|
627
|
+
* `arr` - An array to iterate over.
|
628
|
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
629
|
+
The iterator is passed a `callback(err, sortValue)` which must be called once it
|
630
|
+
has completed with an error (which can be `null`) and a value to use as the sort
|
631
|
+
criteria.
|
632
|
+
* `callback(err, results)` - *Optional* A callback which is called after all the `iterator`
|
633
|
+
functions have finished, or an error occurs. Results is the items from
|
634
|
+
the original `arr` sorted by the values returned by the `iterator` calls.
|
635
|
+
|
636
|
+
__Example__
|
637
|
+
|
638
|
+
```js
|
639
|
+
async.sortBy(['file1','file2','file3'], function(file, callback){
|
640
|
+
fs.stat(file, function(err, stats){
|
641
|
+
callback(err, stats.mtime);
|
642
|
+
});
|
643
|
+
}, function(err, results){
|
644
|
+
// results is now the original array of files sorted by
|
645
|
+
// modified date
|
646
|
+
});
|
647
|
+
```
|
648
|
+
|
649
|
+
__Sort Order__
|
650
|
+
|
651
|
+
By modifying the callback parameter the sorting order can be influenced:
|
652
|
+
|
653
|
+
```js
|
654
|
+
//ascending order
|
655
|
+
async.sortBy([1,9,3,5], function(x, callback){
|
656
|
+
callback(null, x);
|
657
|
+
}, function(err,result){
|
658
|
+
//result callback
|
659
|
+
} );
|
660
|
+
|
661
|
+
//descending order
|
662
|
+
async.sortBy([1,9,3,5], function(x, callback){
|
663
|
+
callback(null, x*-1); //<- x*-1 instead of x, turns the order around
|
664
|
+
}, function(err,result){
|
665
|
+
//result callback
|
666
|
+
} );
|
667
|
+
```
|
668
|
+
|
669
|
+
---------------------------------------
|
670
|
+
|
671
|
+
<a name="some" />
|
672
|
+
### some(arr, iterator, [callback])
|
673
|
+
|
674
|
+
__Alias:__ `any`
|
675
|
+
|
676
|
+
Returns `true` if at least one element in the `arr` satisfies an async test.
|
677
|
+
_The callback for each iterator call only accepts a single argument of `true` or
|
678
|
+
`false`; it does not accept an error argument first!_ This is in-line with the
|
679
|
+
way node libraries work with truth tests like `fs.exists`. Once any iterator
|
680
|
+
call returns `true`, the main `callback` is immediately called.
|
681
|
+
|
682
|
+
__Arguments__
|
683
|
+
|
684
|
+
* `arr` - An array to iterate over.
|
685
|
+
* `iterator(item, callback)` - A truth test to apply to each item in the array
|
686
|
+
in parallel. The iterator is passed a `callback(truthValue)`` which must be
|
687
|
+
called with a boolean argument once it has completed.
|
688
|
+
* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns
|
689
|
+
`true`, or after all the iterator functions have finished. Result will be
|
690
|
+
either `true` or `false` depending on the values of the async tests.
|
691
|
+
|
692
|
+
**Note: the callbacks do not take an error as their first argument.**
|
693
|
+
__Example__
|
694
|
+
|
695
|
+
```js
|
696
|
+
async.some(['file1','file2','file3'], fs.exists, function(result){
|
697
|
+
// if result is true then at least one of the files exists
|
698
|
+
});
|
699
|
+
```
|
700
|
+
|
701
|
+
---------------------------------------
|
702
|
+
|
703
|
+
<a name="every" />
|
704
|
+
### every(arr, iterator, [callback])
|
705
|
+
|
706
|
+
__Alias:__ `all`
|
707
|
+
|
708
|
+
Returns `true` if every element in `arr` satisfies an async test.
|
709
|
+
_The callback for each `iterator` call only accepts a single argument of `true` or
|
710
|
+
`false`; it does not accept an error argument first!_ This is in-line with the
|
711
|
+
way node libraries work with truth tests like `fs.exists`.
|
712
|
+
|
713
|
+
__Arguments__
|
714
|
+
|
715
|
+
* `arr` - An array to iterate over.
|
716
|
+
* `iterator(item, callback)` - A truth test to apply to each item in the array
|
717
|
+
in parallel. The iterator is passed a `callback(truthValue)` which must be
|
718
|
+
called with a boolean argument once it has completed.
|
719
|
+
* `callback(result)` - *Optional* A callback which is called after all the `iterator`
|
720
|
+
functions have finished. Result will be either `true` or `false` depending on
|
721
|
+
the values of the async tests.
|
722
|
+
|
723
|
+
**Note: the callbacks do not take an error as their first argument.**
|
724
|
+
|
725
|
+
__Example__
|
726
|
+
|
727
|
+
```js
|
728
|
+
async.every(['file1','file2','file3'], fs.exists, function(result){
|
729
|
+
// if result is true then every file exists
|
730
|
+
});
|
731
|
+
```
|
732
|
+
|
733
|
+
---------------------------------------
|
734
|
+
|
735
|
+
<a name="concat" />
|
736
|
+
### concat(arr, iterator, [callback])
|
737
|
+
|
738
|
+
Applies `iterator` to each item in `arr`, concatenating the results. Returns the
|
739
|
+
concatenated list. The `iterator`s are called in parallel, and the results are
|
740
|
+
concatenated as they return. There is no guarantee that the results array will
|
741
|
+
be returned in the original order of `arr` passed to the `iterator` function.
|
742
|
+
|
743
|
+
__Arguments__
|
744
|
+
|
745
|
+
* `arr` - An array to iterate over.
|
746
|
+
* `iterator(item, callback)` - A function to apply to each item in `arr`.
|
747
|
+
The iterator is passed a `callback(err, results)` which must be called once it
|
748
|
+
has completed with an error (which can be `null`) and an array of results.
|
749
|
+
* `callback(err, results)` - *Optional* A callback which is called after all the `iterator`
|
750
|
+
functions have finished, or an error occurs. Results is an array containing
|
751
|
+
the concatenated results of the `iterator` function.
|
752
|
+
|
753
|
+
__Example__
|
754
|
+
|
755
|
+
```js
|
756
|
+
async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files){
|
757
|
+
// files is now a list of filenames that exist in the 3 directories
|
758
|
+
});
|
759
|
+
```
|
760
|
+
|
761
|
+
---------------------------------------
|
762
|
+
|
763
|
+
<a name="concatSeries" />
|
764
|
+
### concatSeries(arr, iterator, [callback])
|
765
|
+
|
766
|
+
Same as [`concat`](#concat), but executes in series instead of parallel.
|
767
|
+
|
768
|
+
|
769
|
+
## Control Flow
|
770
|
+
|
771
|
+
<a name="series" />
|
772
|
+
### series(tasks, [callback])
|
773
|
+
|
774
|
+
Run the functions in the `tasks` array in series, each one running once the previous
|
775
|
+
function has completed. If any functions in the series pass an error to its
|
776
|
+
callback, no more functions are run, and `callback` is immediately called with the value of the error.
|
777
|
+
Otherwise, `callback` receives an array of results when `tasks` have completed.
|
778
|
+
|
779
|
+
It is also possible to use an object instead of an array. Each property will be
|
780
|
+
run as a function, and the results will be passed to the final `callback` as an object
|
781
|
+
instead of an array. This can be a more readable way of handling results from
|
782
|
+
[`series`](#series).
|
783
|
+
|
784
|
+
**Note** that while many implementations preserve the order of object properties, the
|
785
|
+
[ECMAScript Language Specifcation](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6)
|
786
|
+
explicitly states that
|
787
|
+
|
788
|
+
> The mechanics and order of enumerating the properties is not specified.
|
789
|
+
|
790
|
+
So if you rely on the order in which your series of functions are executed, and want
|
791
|
+
this to work on all platforms, consider using an array.
|
792
|
+
|
793
|
+
__Arguments__
|
794
|
+
|
795
|
+
* `tasks` - An array or object containing functions to run, each function is passed
|
796
|
+
a `callback(err, result)` it must call on completion with an error `err` (which can
|
797
|
+
be `null`) and an optional `result` value.
|
798
|
+
* `callback(err, results)` - An optional callback to run once all the functions
|
799
|
+
have completed. This function gets a results array (or object) containing all
|
800
|
+
the result arguments passed to the `task` callbacks.
|
801
|
+
|
802
|
+
__Example__
|
803
|
+
|
804
|
+
```js
|
805
|
+
async.series([
|
806
|
+
function(callback){
|
807
|
+
// do some stuff ...
|
808
|
+
callback(null, 'one');
|
809
|
+
},
|
810
|
+
function(callback){
|
811
|
+
// do some more stuff ...
|
812
|
+
callback(null, 'two');
|
813
|
+
}
|
814
|
+
],
|
815
|
+
// optional callback
|
816
|
+
function(err, results){
|
817
|
+
// results is now equal to ['one', 'two']
|
818
|
+
});
|
819
|
+
|
820
|
+
|
821
|
+
// an example using an object instead of an array
|
822
|
+
async.series({
|
823
|
+
one: function(callback){
|
824
|
+
setTimeout(function(){
|
825
|
+
callback(null, 1);
|
826
|
+
}, 200);
|
827
|
+
},
|
828
|
+
two: function(callback){
|
829
|
+
setTimeout(function(){
|
830
|
+
callback(null, 2);
|
831
|
+
}, 100);
|
832
|
+
}
|
833
|
+
},
|
834
|
+
function(err, results) {
|
835
|
+
// results is now equal to: {one: 1, two: 2}
|
836
|
+
});
|
837
|
+
```
|
838
|
+
|
839
|
+
---------------------------------------
|
840
|
+
|
841
|
+
<a name="parallel" />
|
842
|
+
### parallel(tasks, [callback])
|
843
|
+
|
844
|
+
Run the `tasks` array of functions in parallel, without waiting until the previous
|
845
|
+
function has completed. If any of the functions pass an error to its
|
846
|
+
callback, the main `callback` is immediately called with the value of the error.
|
847
|
+
Once the `tasks` have completed, the results are passed to the final `callback` as an
|
848
|
+
array.
|
849
|
+
|
850
|
+
It is also possible to use an object instead of an array. Each property will be
|
851
|
+
run as a function and the results will be passed to the final `callback` as an object
|
852
|
+
instead of an array. This can be a more readable way of handling results from
|
853
|
+
[`parallel`](#parallel).
|
854
|
+
|
855
|
+
|
856
|
+
__Arguments__
|
857
|
+
|
858
|
+
* `tasks` - An array or object containing functions to run. Each function is passed
|
859
|
+
a `callback(err, result)` which it must call on completion with an error `err`
|
860
|
+
(which can be `null`) and an optional `result` value.
|
861
|
+
* `callback(err, results)` - An optional callback to run once all the functions
|
862
|
+
have completed. This function gets a results array (or object) containing all
|
863
|
+
the result arguments passed to the task callbacks.
|
864
|
+
|
865
|
+
__Example__
|
866
|
+
|
867
|
+
```js
|
868
|
+
async.parallel([
|
869
|
+
function(callback){
|
870
|
+
setTimeout(function(){
|
871
|
+
callback(null, 'one');
|
872
|
+
}, 200);
|
873
|
+
},
|
874
|
+
function(callback){
|
875
|
+
setTimeout(function(){
|
876
|
+
callback(null, 'two');
|
877
|
+
}, 100);
|
878
|
+
}
|
879
|
+
],
|
880
|
+
// optional callback
|
881
|
+
function(err, results){
|
882
|
+
// the results array will equal ['one','two'] even though
|
883
|
+
// the second function had a shorter timeout.
|
884
|
+
});
|
885
|
+
|
886
|
+
|
887
|
+
// an example using an object instead of an array
|
888
|
+
async.parallel({
|
889
|
+
one: function(callback){
|
890
|
+
setTimeout(function(){
|
891
|
+
callback(null, 1);
|
892
|
+
}, 200);
|
893
|
+
},
|
894
|
+
two: function(callback){
|
895
|
+
setTimeout(function(){
|
896
|
+
callback(null, 2);
|
897
|
+
}, 100);
|
898
|
+
}
|
899
|
+
},
|
900
|
+
function(err, results) {
|
901
|
+
// results is now equals to: {one: 1, two: 2}
|
902
|
+
});
|
903
|
+
```
|
904
|
+
|
905
|
+
---------------------------------------
|
906
|
+
|
907
|
+
<a name="parallelLimit" />
|
908
|
+
### parallelLimit(tasks, limit, [callback])
|
909
|
+
|
910
|
+
The same as [`parallel`](#parallel), only `tasks` are executed in parallel
|
911
|
+
with a maximum of `limit` tasks executing at any time.
|
912
|
+
|
913
|
+
Note that the `tasks` are not executed in batches, so there is no guarantee that
|
914
|
+
the first `limit` tasks will complete before any others are started.
|
915
|
+
|
916
|
+
__Arguments__
|
917
|
+
|
918
|
+
* `tasks` - An array or object containing functions to run, each function is passed
|
919
|
+
a `callback(err, result)` it must call on completion with an error `err` (which can
|
920
|
+
be `null`) and an optional `result` value.
|
921
|
+
* `limit` - The maximum number of `tasks` to run at any time.
|
922
|
+
* `callback(err, results)` - An optional callback to run once all the functions
|
923
|
+
have completed. This function gets a results array (or object) containing all
|
924
|
+
the result arguments passed to the `task` callbacks.
|
925
|
+
|
926
|
+
---------------------------------------
|
927
|
+
|
928
|
+
<a name="whilst" />
|
929
|
+
### whilst(test, fn, callback)
|
930
|
+
|
931
|
+
Repeatedly call `fn`, while `test` returns `true`. Calls `callback` when stopped,
|
932
|
+
or an error occurs.
|
933
|
+
|
934
|
+
__Arguments__
|
935
|
+
|
936
|
+
* `test()` - synchronous truth test to perform before each execution of `fn`.
|
937
|
+
* `fn(callback)` - A function which is called each time `test` passes. The function is
|
938
|
+
passed a `callback(err)`, which must be called once it has completed with an
|
939
|
+
optional `err` argument.
|
940
|
+
* `callback(err)` - A callback which is called after the test fails and repeated
|
941
|
+
execution of `fn` has stopped.
|
942
|
+
|
943
|
+
__Example__
|
944
|
+
|
945
|
+
```js
|
946
|
+
var count = 0;
|
947
|
+
|
948
|
+
async.whilst(
|
949
|
+
function () { return count < 5; },
|
950
|
+
function (callback) {
|
951
|
+
count++;
|
952
|
+
setTimeout(callback, 1000);
|
953
|
+
},
|
954
|
+
function (err) {
|
955
|
+
// 5 seconds have passed
|
956
|
+
}
|
957
|
+
);
|
958
|
+
```
|
959
|
+
|
960
|
+
---------------------------------------
|
961
|
+
|
962
|
+
<a name="doWhilst" />
|
963
|
+
### doWhilst(fn, test, callback)
|
964
|
+
|
965
|
+
The post-check version of [`whilst`](#whilst). To reflect the difference in
|
966
|
+
the order of operations, the arguments `test` and `fn` are switched.
|
967
|
+
|
968
|
+
`doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
|
969
|
+
|
970
|
+
---------------------------------------
|
971
|
+
|
972
|
+
<a name="until" />
|
973
|
+
### until(test, fn, callback)
|
974
|
+
|
975
|
+
Repeatedly call `fn` until `test` returns `true`. Calls `callback` when stopped,
|
976
|
+
or an error occurs.
|
977
|
+
|
978
|
+
The inverse of [`whilst`](#whilst).
|
979
|
+
|
980
|
+
---------------------------------------
|
981
|
+
|
982
|
+
<a name="doUntil" />
|
983
|
+
### doUntil(fn, test, callback)
|
984
|
+
|
985
|
+
Like [`doWhilst`](#doWhilst), except the `test` is inverted. Note the argument ordering differs from `until`.
|
986
|
+
|
987
|
+
---------------------------------------
|
988
|
+
|
989
|
+
<a name="forever" />
|
990
|
+
### forever(fn, [errback])
|
991
|
+
|
992
|
+
Calls the asynchronous function `fn` with a callback parameter that allows it to
|
993
|
+
call itself again, in series, indefinitely.
|
994
|
+
|
995
|
+
If an error is passed to the callback then `errback` is called with the
|
996
|
+
error, and execution stops, otherwise it will never be called.
|
997
|
+
|
998
|
+
```js
|
999
|
+
async.forever(
|
1000
|
+
function(next) {
|
1001
|
+
// next is suitable for passing to things that need a callback(err [, whatever]);
|
1002
|
+
// it will result in this function being called again.
|
1003
|
+
},
|
1004
|
+
function(err) {
|
1005
|
+
// if next is called with a value in its first parameter, it will appear
|
1006
|
+
// in here as 'err', and execution will stop.
|
1007
|
+
}
|
1008
|
+
);
|
1009
|
+
```
|
1010
|
+
|
1011
|
+
---------------------------------------
|
1012
|
+
|
1013
|
+
<a name="waterfall" />
|
1014
|
+
### waterfall(tasks, [callback])
|
1015
|
+
|
1016
|
+
Runs the `tasks` array of functions in series, each passing their results to the next in
|
1017
|
+
the array. However, if any of the `tasks` pass an error to their own callback, the
|
1018
|
+
next function is not executed, and the main `callback` is immediately called with
|
1019
|
+
the error.
|
1020
|
+
|
1021
|
+
__Arguments__
|
1022
|
+
|
1023
|
+
* `tasks` - An array of functions to run, each function is passed a
|
1024
|
+
`callback(err, result1, result2, ...)` it must call on completion. The first
|
1025
|
+
argument is an error (which can be `null`) and any further arguments will be
|
1026
|
+
passed as arguments in order to the next task.
|
1027
|
+
* `callback(err, [results])` - An optional callback to run once all the functions
|
1028
|
+
have completed. This will be passed the results of the last task's callback.
|
1029
|
+
|
1030
|
+
|
1031
|
+
|
1032
|
+
__Example__
|
1033
|
+
|
1034
|
+
```js
|
1035
|
+
async.waterfall([
|
1036
|
+
function(callback) {
|
1037
|
+
callback(null, 'one', 'two');
|
1038
|
+
},
|
1039
|
+
function(arg1, arg2, callback) {
|
1040
|
+
// arg1 now equals 'one' and arg2 now equals 'two'
|
1041
|
+
callback(null, 'three');
|
1042
|
+
},
|
1043
|
+
function(arg1, callback) {
|
1044
|
+
// arg1 now equals 'three'
|
1045
|
+
callback(null, 'done');
|
1046
|
+
}
|
1047
|
+
], function (err, result) {
|
1048
|
+
// result now equals 'done'
|
1049
|
+
});
|
1050
|
+
```
|
1051
|
+
|
1052
|
+
---------------------------------------
|
1053
|
+
<a name="compose" />
|
1054
|
+
### compose(fn1, fn2...)
|
1055
|
+
|
1056
|
+
Creates a function which is a composition of the passed asynchronous
|
1057
|
+
functions. Each function consumes the return value of the function that
|
1058
|
+
follows. Composing functions `f()`, `g()`, and `h()` would produce the result of
|
1059
|
+
`f(g(h()))`, only this version uses callbacks to obtain the return values.
|
1060
|
+
|
1061
|
+
Each function is executed with the `this` binding of the composed function.
|
1062
|
+
|
1063
|
+
__Arguments__
|
1064
|
+
|
1065
|
+
* `functions...` - the asynchronous functions to compose
|
1066
|
+
|
1067
|
+
|
1068
|
+
__Example__
|
1069
|
+
|
1070
|
+
```js
|
1071
|
+
function add1(n, callback) {
|
1072
|
+
setTimeout(function () {
|
1073
|
+
callback(null, n + 1);
|
1074
|
+
}, 10);
|
1075
|
+
}
|
1076
|
+
|
1077
|
+
function mul3(n, callback) {
|
1078
|
+
setTimeout(function () {
|
1079
|
+
callback(null, n * 3);
|
1080
|
+
}, 10);
|
1081
|
+
}
|
1082
|
+
|
1083
|
+
var add1mul3 = async.compose(mul3, add1);
|
1084
|
+
|
1085
|
+
add1mul3(4, function (err, result) {
|
1086
|
+
// result now equals 15
|
1087
|
+
});
|
1088
|
+
```
|
1089
|
+
|
1090
|
+
---------------------------------------
|
1091
|
+
<a name="seq" />
|
1092
|
+
### seq(fn1, fn2...)
|
1093
|
+
|
1094
|
+
Version of the compose function that is more natural to read.
|
1095
|
+
Each function consumes the return value of the previous function.
|
1096
|
+
It is the equivalent of [`compose`](#compose) with the arguments reversed.
|
1097
|
+
|
1098
|
+
Each function is executed with the `this` binding of the composed function.
|
1099
|
+
|
1100
|
+
__Arguments__
|
1101
|
+
|
1102
|
+
* functions... - the asynchronous functions to compose
|
1103
|
+
|
1104
|
+
|
1105
|
+
__Example__
|
1106
|
+
|
1107
|
+
```js
|
1108
|
+
// Requires lodash (or underscore), express3 and dresende's orm2.
|
1109
|
+
// Part of an app, that fetches cats of the logged user.
|
1110
|
+
// This example uses `seq` function to avoid overnesting and error
|
1111
|
+
// handling clutter.
|
1112
|
+
app.get('/cats', function(request, response) {
|
1113
|
+
var User = request.models.User;
|
1114
|
+
async.seq(
|
1115
|
+
_.bind(User.get, User), // 'User.get' has signature (id, callback(err, data))
|
1116
|
+
function(user, fn) {
|
1117
|
+
user.getCats(fn); // 'getCats' has signature (callback(err, data))
|
1118
|
+
}
|
1119
|
+
)(req.session.user_id, function (err, cats) {
|
1120
|
+
if (err) {
|
1121
|
+
console.error(err);
|
1122
|
+
response.json({ status: 'error', message: err.message });
|
1123
|
+
} else {
|
1124
|
+
response.json({ status: 'ok', message: 'Cats found', data: cats });
|
1125
|
+
}
|
1126
|
+
});
|
1127
|
+
});
|
1128
|
+
```
|
1129
|
+
|
1130
|
+
---------------------------------------
|
1131
|
+
<a name="applyEach" />
|
1132
|
+
### applyEach(fns, args..., callback)
|
1133
|
+
|
1134
|
+
Applies the provided arguments to each function in the array, calling
|
1135
|
+
`callback` after all functions have completed. If you only provide the first
|
1136
|
+
argument, then it will return a function which lets you pass in the
|
1137
|
+
arguments as if it were a single function call.
|
1138
|
+
|
1139
|
+
__Arguments__
|
1140
|
+
|
1141
|
+
* `fns` - the asynchronous functions to all call with the same arguments
|
1142
|
+
* `args...` - any number of separate arguments to pass to the function
|
1143
|
+
* `callback` - the final argument should be the callback, called when all
|
1144
|
+
functions have completed processing
|
1145
|
+
|
1146
|
+
|
1147
|
+
__Example__
|
1148
|
+
|
1149
|
+
```js
|
1150
|
+
async.applyEach([enableSearch, updateSchema], 'bucket', callback);
|
1151
|
+
|
1152
|
+
// partial application example:
|
1153
|
+
async.each(
|
1154
|
+
buckets,
|
1155
|
+
async.applyEach([enableSearch, updateSchema]),
|
1156
|
+
callback
|
1157
|
+
);
|
1158
|
+
```
|
1159
|
+
|
1160
|
+
---------------------------------------
|
1161
|
+
|
1162
|
+
<a name="applyEachSeries" />
|
1163
|
+
### applyEachSeries(arr, args..., callback)
|
1164
|
+
|
1165
|
+
The same as [`applyEach`](#applyEach) only the functions are applied in series.
|
1166
|
+
|
1167
|
+
---------------------------------------
|
1168
|
+
|
1169
|
+
<a name="queue" />
|
1170
|
+
### queue(worker, [concurrency])
|
1171
|
+
|
1172
|
+
Creates a `queue` object with the specified `concurrency`. Tasks added to the
|
1173
|
+
`queue` are processed in parallel (up to the `concurrency` limit). If all
|
1174
|
+
`worker`s are in progress, the task is queued until one becomes available.
|
1175
|
+
Once a `worker` completes a `task`, that `task`'s callback is called.
|
1176
|
+
|
1177
|
+
__Arguments__
|
1178
|
+
|
1179
|
+
* `worker(task, callback)` - An asynchronous function for processing a queued
|
1180
|
+
task, which must call its `callback(err)` argument when finished, with an
|
1181
|
+
optional `error` as an argument. If you want to handle errors from an individual task, pass a callback to `q.push()`.
|
1182
|
+
* `concurrency` - An `integer` for determining how many `worker` functions should be
|
1183
|
+
run in parallel. If omitted, the concurrency defaults to `1`. If the concurrency is `0`, an error is thrown.
|
1184
|
+
|
1185
|
+
__Queue objects__
|
1186
|
+
|
1187
|
+
The `queue` object returned by this function has the following properties and
|
1188
|
+
methods:
|
1189
|
+
|
1190
|
+
* `length()` - a function returning the number of items waiting to be processed.
|
1191
|
+
* `started` - a function returning whether or not any items have been pushed and processed by the queue
|
1192
|
+
* `running()` - a function returning the number of items currently being processed.
|
1193
|
+
* `idle()` - a function returning false if there are items waiting or being processed, or true if not.
|
1194
|
+
* `concurrency` - an integer for determining how many `worker` functions should be
|
1195
|
+
run in parallel. This property can be changed after a `queue` is created to
|
1196
|
+
alter the concurrency on-the-fly.
|
1197
|
+
* `push(task, [callback])` - add a new task to the `queue`. Calls `callback` once
|
1198
|
+
the `worker` has finished processing the task. Instead of a single task, a `tasks` array
|
1199
|
+
can be submitted. The respective callback is used for every task in the list.
|
1200
|
+
* `unshift(task, [callback])` - add a new task to the front of the `queue`.
|
1201
|
+
* `saturated` - a callback that is called when the `queue` length hits the `concurrency` limit,
|
1202
|
+
and further tasks will be queued.
|
1203
|
+
* `empty` - a callback that is called when the last item from the `queue` is given to a `worker`.
|
1204
|
+
* `drain` - a callback that is called when the last item from the `queue` has returned from the `worker`.
|
1205
|
+
* `paused` - a boolean for determining whether the queue is in a paused state
|
1206
|
+
* `pause()` - a function that pauses the processing of tasks until `resume()` is called.
|
1207
|
+
* `resume()` - a function that resumes the processing of queued tasks when the queue is paused.
|
1208
|
+
* `kill()` - a function that removes the `drain` callback and empties remaining tasks from the queue forcing it to go idle.
|
1209
|
+
|
1210
|
+
__Example__
|
1211
|
+
|
1212
|
+
```js
|
1213
|
+
// create a queue object with concurrency 2
|
1214
|
+
|
1215
|
+
var q = async.queue(function (task, callback) {
|
1216
|
+
console.log('hello ' + task.name);
|
1217
|
+
callback();
|
1218
|
+
}, 2);
|
1219
|
+
|
1220
|
+
|
1221
|
+
// assign a callback
|
1222
|
+
q.drain = function() {
|
1223
|
+
console.log('all items have been processed');
|
1224
|
+
}
|
1225
|
+
|
1226
|
+
// add some items to the queue
|
1227
|
+
|
1228
|
+
q.push({name: 'foo'}, function (err) {
|
1229
|
+
console.log('finished processing foo');
|
1230
|
+
});
|
1231
|
+
q.push({name: 'bar'}, function (err) {
|
1232
|
+
console.log('finished processing bar');
|
1233
|
+
});
|
1234
|
+
|
1235
|
+
// add some items to the queue (batch-wise)
|
1236
|
+
|
1237
|
+
q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function (err) {
|
1238
|
+
console.log('finished processing item');
|
1239
|
+
});
|
1240
|
+
|
1241
|
+
// add some items to the front of the queue
|
1242
|
+
|
1243
|
+
q.unshift({name: 'bar'}, function (err) {
|
1244
|
+
console.log('finished processing bar');
|
1245
|
+
});
|
1246
|
+
```
|
1247
|
+
|
1248
|
+
|
1249
|
+
---------------------------------------
|
1250
|
+
|
1251
|
+
<a name="priorityQueue" />
|
1252
|
+
### priorityQueue(worker, concurrency)
|
1253
|
+
|
1254
|
+
The same as [`queue`](#queue) only tasks are assigned a priority and completed in ascending priority order. There are two differences between `queue` and `priorityQueue` objects:
|
1255
|
+
|
1256
|
+
* `push(task, priority, [callback])` - `priority` should be a number. If an array of
|
1257
|
+
`tasks` is given, all tasks will be assigned the same priority.
|
1258
|
+
* The `unshift` method was removed.
|
1259
|
+
|
1260
|
+
---------------------------------------
|
1261
|
+
|
1262
|
+
<a name="cargo" />
|
1263
|
+
### cargo(worker, [payload])
|
1264
|
+
|
1265
|
+
Creates a `cargo` object with the specified payload. Tasks added to the
|
1266
|
+
cargo will be processed altogether (up to the `payload` limit). If the
|
1267
|
+
`worker` is in progress, the task is queued until it becomes available. Once
|
1268
|
+
the `worker` has completed some tasks, each callback of those tasks is called.
|
1269
|
+
Check out [these](https://camo.githubusercontent.com/6bbd36f4cf5b35a0f11a96dcd2e97711ffc2fb37/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130382f62626330636662302d356632392d313165322d393734662d3333393763363464633835382e676966) [animations](https://camo.githubusercontent.com/f4810e00e1c5f5f8addbe3e9f49064fd5d102699/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130312f38346339323036362d356632392d313165322d383134662d3964336430323431336266642e676966) for how `cargo` and `queue` work.
|
1270
|
+
|
1271
|
+
While [queue](#queue) passes only one task to one of a group of workers
|
1272
|
+
at a time, cargo passes an array of tasks to a single worker, repeating
|
1273
|
+
when the worker is finished.
|
1274
|
+
|
1275
|
+
__Arguments__
|
1276
|
+
|
1277
|
+
* `worker(tasks, callback)` - An asynchronous function for processing an array of
|
1278
|
+
queued tasks, which must call its `callback(err)` argument when finished, with
|
1279
|
+
an optional `err` argument.
|
1280
|
+
* `payload` - An optional `integer` for determining how many tasks should be
|
1281
|
+
processed per round; if omitted, the default is unlimited.
|
1282
|
+
|
1283
|
+
__Cargo objects__
|
1284
|
+
|
1285
|
+
The `cargo` object returned by this function has the following properties and
|
1286
|
+
methods:
|
1287
|
+
|
1288
|
+
* `length()` - A function returning the number of items waiting to be processed.
|
1289
|
+
* `payload` - An `integer` for determining how many tasks should be
|
1290
|
+
process per round. This property can be changed after a `cargo` is created to
|
1291
|
+
alter the payload on-the-fly.
|
1292
|
+
* `push(task, [callback])` - Adds `task` to the `queue`. The callback is called
|
1293
|
+
once the `worker` has finished processing the task. Instead of a single task, an array of `tasks`
|
1294
|
+
can be submitted. The respective callback is used for every task in the list.
|
1295
|
+
* `saturated` - A callback that is called when the `queue.length()` hits the concurrency and further tasks will be queued.
|
1296
|
+
* `empty` - A callback that is called when the last item from the `queue` is given to a `worker`.
|
1297
|
+
* `drain` - A callback that is called when the last item from the `queue` has returned from the `worker`.
|
1298
|
+
* `idle()`, `pause()`, `resume()`, `kill()` - cargo inherits all of the same methods and event calbacks as [`queue`](#queue)
|
1299
|
+
|
1300
|
+
__Example__
|
1301
|
+
|
1302
|
+
```js
|
1303
|
+
// create a cargo object with payload 2
|
1304
|
+
|
1305
|
+
var cargo = async.cargo(function (tasks, callback) {
|
1306
|
+
for(var i=0; i<tasks.length; i++){
|
1307
|
+
console.log('hello ' + tasks[i].name);
|
1308
|
+
}
|
1309
|
+
callback();
|
1310
|
+
}, 2);
|
1311
|
+
|
1312
|
+
|
1313
|
+
// add some items
|
1314
|
+
|
1315
|
+
cargo.push({name: 'foo'}, function (err) {
|
1316
|
+
console.log('finished processing foo');
|
1317
|
+
});
|
1318
|
+
cargo.push({name: 'bar'}, function (err) {
|
1319
|
+
console.log('finished processing bar');
|
1320
|
+
});
|
1321
|
+
cargo.push({name: 'baz'}, function (err) {
|
1322
|
+
console.log('finished processing baz');
|
1323
|
+
});
|
1324
|
+
```
|
1325
|
+
|
1326
|
+
---------------------------------------
|
1327
|
+
|
1328
|
+
<a name="auto" />
|
1329
|
+
### auto(tasks, [callback])
|
1330
|
+
|
1331
|
+
Determines the best order for running the functions in `tasks`, based on their
|
1332
|
+
requirements. Each function can optionally depend on other functions being completed
|
1333
|
+
first, and each function is run as soon as its requirements are satisfied.
|
1334
|
+
|
1335
|
+
If any of the functions pass an error to their callback, it will not
|
1336
|
+
complete (so any other functions depending on it will not run), and the main
|
1337
|
+
`callback` is immediately called with the error. Functions also receive an
|
1338
|
+
object containing the results of functions which have completed so far.
|
1339
|
+
|
1340
|
+
Note, all functions are called with a `results` object as a second argument,
|
1341
|
+
so it is unsafe to pass functions in the `tasks` object which cannot handle the
|
1342
|
+
extra argument.
|
1343
|
+
|
1344
|
+
For example, this snippet of code:
|
1345
|
+
|
1346
|
+
```js
|
1347
|
+
async.auto({
|
1348
|
+
readData: async.apply(fs.readFile, 'data.txt', 'utf-8')
|
1349
|
+
}, callback);
|
1350
|
+
```
|
1351
|
+
|
1352
|
+
will have the effect of calling `readFile` with the results object as the last
|
1353
|
+
argument, which will fail:
|
1354
|
+
|
1355
|
+
```js
|
1356
|
+
fs.readFile('data.txt', 'utf-8', cb, {});
|
1357
|
+
```
|
1358
|
+
|
1359
|
+
Instead, wrap the call to `readFile` in a function which does not forward the
|
1360
|
+
`results` object:
|
1361
|
+
|
1362
|
+
```js
|
1363
|
+
async.auto({
|
1364
|
+
readData: function(cb, results){
|
1365
|
+
fs.readFile('data.txt', 'utf-8', cb);
|
1366
|
+
}
|
1367
|
+
}, callback);
|
1368
|
+
```
|
1369
|
+
|
1370
|
+
__Arguments__
|
1371
|
+
|
1372
|
+
* `tasks` - An object. Each of its properties is either a function or an array of
|
1373
|
+
requirements, with the function itself the last item in the array. The object's key
|
1374
|
+
of a property serves as the name of the task defined by that property,
|
1375
|
+
i.e. can be used when specifying requirements for other tasks.
|
1376
|
+
The function receives two arguments: (1) a `callback(err, result)` which must be
|
1377
|
+
called when finished, passing an `error` (which can be `null`) and the result of
|
1378
|
+
the function's execution, and (2) a `results` object, containing the results of
|
1379
|
+
the previously executed functions.
|
1380
|
+
* `callback(err, results)` - An optional callback which is called when all the
|
1381
|
+
tasks have been completed. It receives the `err` argument if any `tasks`
|
1382
|
+
pass an error to their callback. Results are always returned; however, if
|
1383
|
+
an error occurs, no further `tasks` will be performed, and the results
|
1384
|
+
object will only contain partial results.
|
1385
|
+
|
1386
|
+
|
1387
|
+
__Example__
|
1388
|
+
|
1389
|
+
```js
|
1390
|
+
async.auto({
|
1391
|
+
get_data: function(callback){
|
1392
|
+
console.log('in get_data');
|
1393
|
+
// async code to get some data
|
1394
|
+
callback(null, 'data', 'converted to array');
|
1395
|
+
},
|
1396
|
+
make_folder: function(callback){
|
1397
|
+
console.log('in make_folder');
|
1398
|
+
// async code to create a directory to store a file in
|
1399
|
+
// this is run at the same time as getting the data
|
1400
|
+
callback(null, 'folder');
|
1401
|
+
},
|
1402
|
+
write_file: ['get_data', 'make_folder', function(callback, results){
|
1403
|
+
console.log('in write_file', JSON.stringify(results));
|
1404
|
+
// once there is some data and the directory exists,
|
1405
|
+
// write the data to a file in the directory
|
1406
|
+
callback(null, 'filename');
|
1407
|
+
}],
|
1408
|
+
email_link: ['write_file', function(callback, results){
|
1409
|
+
console.log('in email_link', JSON.stringify(results));
|
1410
|
+
// once the file is written let's email a link to it...
|
1411
|
+
// results.write_file contains the filename returned by write_file.
|
1412
|
+
callback(null, {'file':results.write_file, 'email':'user@example.com'});
|
1413
|
+
}]
|
1414
|
+
}, function(err, results) {
|
1415
|
+
console.log('err = ', err);
|
1416
|
+
console.log('results = ', results);
|
1417
|
+
});
|
1418
|
+
```
|
1419
|
+
|
1420
|
+
This is a fairly trivial example, but to do this using the basic parallel and
|
1421
|
+
series functions would look like this:
|
1422
|
+
|
1423
|
+
```js
|
1424
|
+
async.parallel([
|
1425
|
+
function(callback){
|
1426
|
+
console.log('in get_data');
|
1427
|
+
// async code to get some data
|
1428
|
+
callback(null, 'data', 'converted to array');
|
1429
|
+
},
|
1430
|
+
function(callback){
|
1431
|
+
console.log('in make_folder');
|
1432
|
+
// async code to create a directory to store a file in
|
1433
|
+
// this is run at the same time as getting the data
|
1434
|
+
callback(null, 'folder');
|
1435
|
+
}
|
1436
|
+
],
|
1437
|
+
function(err, results){
|
1438
|
+
async.series([
|
1439
|
+
function(callback){
|
1440
|
+
console.log('in write_file', JSON.stringify(results));
|
1441
|
+
// once there is some data and the directory exists,
|
1442
|
+
// write the data to a file in the directory
|
1443
|
+
results.push('filename');
|
1444
|
+
callback(null);
|
1445
|
+
},
|
1446
|
+
function(callback){
|
1447
|
+
console.log('in email_link', JSON.stringify(results));
|
1448
|
+
// once the file is written let's email a link to it...
|
1449
|
+
callback(null, {'file':results.pop(), 'email':'user@example.com'});
|
1450
|
+
}
|
1451
|
+
]);
|
1452
|
+
});
|
1453
|
+
```
|
1454
|
+
|
1455
|
+
For a complicated series of `async` tasks, using the [`auto`](#auto) function makes adding
|
1456
|
+
new tasks much easier (and the code more readable).
|
1457
|
+
|
1458
|
+
|
1459
|
+
---------------------------------------
|
1460
|
+
|
1461
|
+
<a name="retry" />
|
1462
|
+
### retry([times = 5], task, [callback])
|
1463
|
+
|
1464
|
+
Attempts to get a successful response from `task` no more than `times` times before
|
1465
|
+
returning an error. If the task is successful, the `callback` will be passed the result
|
1466
|
+
of the successful task. If all attempts fail, the callback will be passed the error and
|
1467
|
+
result (if any) of the final attempt.
|
1468
|
+
|
1469
|
+
__Arguments__
|
1470
|
+
|
1471
|
+
* `times` - An integer indicating how many times to attempt the `task` before giving up. Defaults to 5.
|
1472
|
+
* `task(callback, results)` - A function which receives two arguments: (1) a `callback(err, result)`
|
1473
|
+
which must be called when finished, passing `err` (which can be `null`) and the `result` of
|
1474
|
+
the function's execution, and (2) a `results` object, containing the results of
|
1475
|
+
the previously executed functions (if nested inside another control flow).
|
1476
|
+
* `callback(err, results)` - An optional callback which is called when the
|
1477
|
+
task has succeeded, or after the final failed attempt. It receives the `err` and `result` arguments of the last attempt at completing the `task`.
|
1478
|
+
|
1479
|
+
The [`retry`](#retry) function can be used as a stand-alone control flow by passing a
|
1480
|
+
callback, as shown below:
|
1481
|
+
|
1482
|
+
```js
|
1483
|
+
async.retry(3, apiMethod, function(err, result) {
|
1484
|
+
// do something with the result
|
1485
|
+
});
|
1486
|
+
```
|
1487
|
+
|
1488
|
+
It can also be embeded within other control flow functions to retry individual methods
|
1489
|
+
that are not as reliable, like this:
|
1490
|
+
|
1491
|
+
```js
|
1492
|
+
async.auto({
|
1493
|
+
users: api.getUsers.bind(api),
|
1494
|
+
payments: async.retry(3, api.getPayments.bind(api))
|
1495
|
+
}, function(err, results) {
|
1496
|
+
// do something with the results
|
1497
|
+
});
|
1498
|
+
```
|
1499
|
+
|
1500
|
+
|
1501
|
+
---------------------------------------
|
1502
|
+
|
1503
|
+
<a name="iterator" />
|
1504
|
+
### iterator(tasks)
|
1505
|
+
|
1506
|
+
Creates an iterator function which calls the next function in the `tasks` array,
|
1507
|
+
returning a continuation to call the next one after that. It's also possible to
|
1508
|
+
“peek” at the next iterator with `iterator.next()`.
|
1509
|
+
|
1510
|
+
This function is used internally by the `async` module, but can be useful when
|
1511
|
+
you want to manually control the flow of functions in series.
|
1512
|
+
|
1513
|
+
__Arguments__
|
1514
|
+
|
1515
|
+
* `tasks` - An array of functions to run.
|
1516
|
+
|
1517
|
+
__Example__
|
1518
|
+
|
1519
|
+
```js
|
1520
|
+
var iterator = async.iterator([
|
1521
|
+
function(){ sys.p('one'); },
|
1522
|
+
function(){ sys.p('two'); },
|
1523
|
+
function(){ sys.p('three'); }
|
1524
|
+
]);
|
1525
|
+
|
1526
|
+
node> var iterator2 = iterator();
|
1527
|
+
'one'
|
1528
|
+
node> var iterator3 = iterator2();
|
1529
|
+
'two'
|
1530
|
+
node> iterator3();
|
1531
|
+
'three'
|
1532
|
+
node> var nextfn = iterator2.next();
|
1533
|
+
node> nextfn();
|
1534
|
+
'three'
|
1535
|
+
```
|
1536
|
+
|
1537
|
+
---------------------------------------
|
1538
|
+
|
1539
|
+
<a name="apply" />
|
1540
|
+
### apply(function, arguments..)
|
1541
|
+
|
1542
|
+
Creates a continuation function with some arguments already applied.
|
1543
|
+
|
1544
|
+
Useful as a shorthand when combined with other control flow functions. Any arguments
|
1545
|
+
passed to the returned function are added to the arguments originally passed
|
1546
|
+
to apply.
|
1547
|
+
|
1548
|
+
__Arguments__
|
1549
|
+
|
1550
|
+
* `function` - The function you want to eventually apply all arguments to.
|
1551
|
+
* `arguments...` - Any number of arguments to automatically apply when the
|
1552
|
+
continuation is called.
|
1553
|
+
|
1554
|
+
__Example__
|
1555
|
+
|
1556
|
+
```js
|
1557
|
+
// using apply
|
1558
|
+
|
1559
|
+
async.parallel([
|
1560
|
+
async.apply(fs.writeFile, 'testfile1', 'test1'),
|
1561
|
+
async.apply(fs.writeFile, 'testfile2', 'test2'),
|
1562
|
+
]);
|
1563
|
+
|
1564
|
+
|
1565
|
+
// the same process without using apply
|
1566
|
+
|
1567
|
+
async.parallel([
|
1568
|
+
function(callback){
|
1569
|
+
fs.writeFile('testfile1', 'test1', callback);
|
1570
|
+
},
|
1571
|
+
function(callback){
|
1572
|
+
fs.writeFile('testfile2', 'test2', callback);
|
1573
|
+
}
|
1574
|
+
]);
|
1575
|
+
```
|
1576
|
+
|
1577
|
+
It's possible to pass any number of additional arguments when calling the
|
1578
|
+
continuation:
|
1579
|
+
|
1580
|
+
```js
|
1581
|
+
node> var fn = async.apply(sys.puts, 'one');
|
1582
|
+
node> fn('two', 'three');
|
1583
|
+
one
|
1584
|
+
two
|
1585
|
+
three
|
1586
|
+
```
|
1587
|
+
|
1588
|
+
---------------------------------------
|
1589
|
+
|
1590
|
+
<a name="nextTick" />
|
1591
|
+
### nextTick(callback), setImmediate(callback)
|
1592
|
+
|
1593
|
+
Calls `callback` on a later loop around the event loop. In Node.js this just
|
1594
|
+
calls `process.nextTick`; in the browser it falls back to `setImmediate(callback)`
|
1595
|
+
if available, otherwise `setTimeout(callback, 0)`, which means other higher priority
|
1596
|
+
events may precede the execution of `callback`.
|
1597
|
+
|
1598
|
+
This is used internally for browser-compatibility purposes.
|
1599
|
+
|
1600
|
+
__Arguments__
|
1601
|
+
|
1602
|
+
* `callback` - The function to call on a later loop around the event loop.
|
1603
|
+
|
1604
|
+
__Example__
|
1605
|
+
|
1606
|
+
```js
|
1607
|
+
var call_order = [];
|
1608
|
+
async.nextTick(function(){
|
1609
|
+
call_order.push('two');
|
1610
|
+
// call_order now equals ['one','two']
|
1611
|
+
});
|
1612
|
+
call_order.push('one')
|
1613
|
+
```
|
1614
|
+
|
1615
|
+
<a name="times" />
|
1616
|
+
### times(n, iterator, [callback])
|
1617
|
+
|
1618
|
+
Calls the `iterator` function `n` times, and accumulates results in the same manner
|
1619
|
+
you would use with [`map`](#map).
|
1620
|
+
|
1621
|
+
__Arguments__
|
1622
|
+
|
1623
|
+
* `n` - The number of times to run the function.
|
1624
|
+
* `iterator` - The function to call `n` times.
|
1625
|
+
* `callback` - see [`map`](#map)
|
1626
|
+
|
1627
|
+
__Example__
|
1628
|
+
|
1629
|
+
```js
|
1630
|
+
// Pretend this is some complicated async factory
|
1631
|
+
var createUser = function(id, callback) {
|
1632
|
+
callback(null, {
|
1633
|
+
id: 'user' + id
|
1634
|
+
})
|
1635
|
+
}
|
1636
|
+
// generate 5 users
|
1637
|
+
async.times(5, function(n, next){
|
1638
|
+
createUser(n, function(err, user) {
|
1639
|
+
next(err, user)
|
1640
|
+
})
|
1641
|
+
}, function(err, users) {
|
1642
|
+
// we should now have 5 users
|
1643
|
+
});
|
1644
|
+
```
|
1645
|
+
|
1646
|
+
<a name="timesSeries" />
|
1647
|
+
### timesSeries(n, iterator, [callback])
|
1648
|
+
|
1649
|
+
The same as [`times`](#times), only the iterator is applied to each item in `arr` in
|
1650
|
+
series. The next `iterator` is only called once the current one has completed.
|
1651
|
+
The results array will be in the same order as the original.
|
1652
|
+
|
1653
|
+
<a name="timesLimit" />
|
1654
|
+
### timesLimit(n, limit, iterator, [callback])
|
1655
|
+
|
1656
|
+
The same as [`times`](#times), except a maximum of `limit` iterators are run at a given time, similar to [`mapLimit`](#mapLimit).
|
1657
|
+
|
1658
|
+
|
1659
|
+
## Utils
|
1660
|
+
|
1661
|
+
<a name="memoize" />
|
1662
|
+
### memoize(fn, [hasher])
|
1663
|
+
|
1664
|
+
Caches the results of an `async` function. When creating a hash to store function
|
1665
|
+
results against, the callback is omitted from the hash and an optional hash
|
1666
|
+
function can be used.
|
1667
|
+
|
1668
|
+
If no hash function is specified, the first argument is used as a hash key, which may work reasonably if it is a string or a data type that converts to a distinct string. Note that objects and arrays will not behave reasonably. Neither will cases where the other arguments are significant. In such cases, specify your own hash function.
|
1669
|
+
|
1670
|
+
The cache of results is exposed as the `memo` property of the function returned
|
1671
|
+
by `memoize`.
|
1672
|
+
|
1673
|
+
__Arguments__
|
1674
|
+
|
1675
|
+
* `fn` - The function to proxy and cache results from.
|
1676
|
+
* `hasher` - An optional function for generating a custom hash for storing
|
1677
|
+
results. It has all the arguments applied to it apart from the callback, and
|
1678
|
+
must be synchronous.
|
1679
|
+
|
1680
|
+
__Example__
|
1681
|
+
|
1682
|
+
```js
|
1683
|
+
var slow_fn = function (name, callback) {
|
1684
|
+
// do something
|
1685
|
+
callback(null, result);
|
1686
|
+
};
|
1687
|
+
var fn = async.memoize(slow_fn);
|
1688
|
+
|
1689
|
+
// fn can now be used as if it were slow_fn
|
1690
|
+
fn('some name', function () {
|
1691
|
+
// callback
|
1692
|
+
});
|
1693
|
+
```
|
1694
|
+
|
1695
|
+
<a name="unmemoize" />
|
1696
|
+
### unmemoize(fn)
|
1697
|
+
|
1698
|
+
Undoes a [`memoize`](#memoize)d function, reverting it to the original, unmemoized
|
1699
|
+
form. Handy for testing.
|
1700
|
+
|
1701
|
+
__Arguments__
|
1702
|
+
|
1703
|
+
* `fn` - the memoized function
|
1704
|
+
|
1705
|
+
---------------------------------------
|
1706
|
+
|
1707
|
+
<a name="ensureAsync" />
|
1708
|
+
### ensureAsync(fn)
|
1709
|
+
|
1710
|
+
Wrap an async function and ensure it calls its callback on a later tick of the event loop. If the function already calls its callback on a next tick, no extra deferral is added. This is useful for preventing stack overflows (`RangeError: Maximum call stack size exceeded`) and generally keeping [Zalgo](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony) contained.
|
1711
|
+
|
1712
|
+
__Arguments__
|
1713
|
+
|
1714
|
+
* `fn` - an async function, one that expects a node-style callback as its last argument
|
1715
|
+
|
1716
|
+
Returns a wrapped function with the exact same call signature as the function passed in.
|
1717
|
+
|
1718
|
+
__Example__
|
1719
|
+
|
1720
|
+
```js
|
1721
|
+
function sometimesAsync(arg, callback) {
|
1722
|
+
if (cache[arg]) {
|
1723
|
+
return callback(null, cache[arg]); // this would be synchronous!!
|
1724
|
+
} else {
|
1725
|
+
doSomeIO(arg, callback); // this IO would be asynchronous
|
1726
|
+
}
|
1727
|
+
}
|
1728
|
+
|
1729
|
+
// this has a risk of stack overflows if many results are cached in a row
|
1730
|
+
async.mapSeries(args, sometimesAsync, done);
|
1731
|
+
|
1732
|
+
// this will defer sometimesAsync's callback if necessary,
|
1733
|
+
// preventing stack overflows
|
1734
|
+
async.mapSeries(args, async.ensureAsync(sometimesAsync), done);
|
1735
|
+
|
1736
|
+
```
|
1737
|
+
|
1738
|
+
---------------------------------------
|
1739
|
+
|
1740
|
+
<a name="log" />
|
1741
|
+
### log(function, arguments)
|
1742
|
+
|
1743
|
+
Logs the result of an `async` function to the `console`. Only works in Node.js or
|
1744
|
+
in browsers that support `console.log` and `console.error` (such as FF and Chrome).
|
1745
|
+
If multiple arguments are returned from the async function, `console.log` is
|
1746
|
+
called on each argument in order.
|
1747
|
+
|
1748
|
+
__Arguments__
|
1749
|
+
|
1750
|
+
* `function` - The function you want to eventually apply all arguments to.
|
1751
|
+
* `arguments...` - Any number of arguments to apply to the function.
|
1752
|
+
|
1753
|
+
__Example__
|
1754
|
+
|
1755
|
+
```js
|
1756
|
+
var hello = function(name, callback){
|
1757
|
+
setTimeout(function(){
|
1758
|
+
callback(null, 'hello ' + name);
|
1759
|
+
}, 1000);
|
1760
|
+
};
|
1761
|
+
```
|
1762
|
+
```js
|
1763
|
+
node> async.log(hello, 'world');
|
1764
|
+
'hello world'
|
1765
|
+
```
|
1766
|
+
|
1767
|
+
---------------------------------------
|
1768
|
+
|
1769
|
+
<a name="dir" />
|
1770
|
+
### dir(function, arguments)
|
1771
|
+
|
1772
|
+
Logs the result of an `async` function to the `console` using `console.dir` to
|
1773
|
+
display the properties of the resulting object. Only works in Node.js or
|
1774
|
+
in browsers that support `console.dir` and `console.error` (such as FF and Chrome).
|
1775
|
+
If multiple arguments are returned from the async function, `console.dir` is
|
1776
|
+
called on each argument in order.
|
1777
|
+
|
1778
|
+
__Arguments__
|
1779
|
+
|
1780
|
+
* `function` - The function you want to eventually apply all arguments to.
|
1781
|
+
* `arguments...` - Any number of arguments to apply to the function.
|
1782
|
+
|
1783
|
+
__Example__
|
1784
|
+
|
1785
|
+
```js
|
1786
|
+
var hello = function(name, callback){
|
1787
|
+
setTimeout(function(){
|
1788
|
+
callback(null, {hello: name});
|
1789
|
+
}, 1000);
|
1790
|
+
};
|
1791
|
+
```
|
1792
|
+
```js
|
1793
|
+
node> async.dir(hello, 'world');
|
1794
|
+
{hello: 'world'}
|
1795
|
+
```
|
1796
|
+
|
1797
|
+
---------------------------------------
|
1798
|
+
|
1799
|
+
<a name="noConflict" />
|
1800
|
+
### noConflict()
|
1801
|
+
|
1802
|
+
Changes the value of `async` back to its original value, returning a reference to the
|
1803
|
+
`async` object.
|