A pausable and resumable queue
A queue for Node.js that can be paused and resumed.
This can become useful if you want to stop processing when a resource becomes unavailable and continue execution when it's available again. Or what ever you can imagine.
npm install pause-queue --save
Or manually add it to your package.json
var pausequeue = require'pause-queue'i = 0start = +Datenowanimals ='Aardvark' 'Albatross''Alligator' 'Alpaca' 'Ant''Anteater' 'Antelope' 'Ape''Armadillo' 'Donkey' 'Baboon''Badger' 'Barracuda' 'Bat''Bear' 'Beaver' 'Bee' 'Bison''Buffalo' ;// Helper function to show the time differencereturn Mathroundtime - start / 1000 + 's';// Create a new queue. The first argument is the worker, the second is the concurrency (default: 1)var animalQueue = pausequeueconsole.loganimal since+Datenow;done; // don't forget1;// Pause the queue after 4 secondssetTimeoutanimalQueuepause /* This is optional and will be called once all pending tasks are complete */ ;4000;// Resume the queue after 10 secondssetTimeoutanimalQueueresume;10000;// Add a new animal to the queue every second until all animals are addedvar interval = setIntervalif i === animalslength return clearIntervalinterval;animalQueuepushanimalsi;i++;1000;
Aardvark 1s Albatross 2s Alligator 3s // ...now waiting for 6 seconds Alpaca 10s Ant 10s Anteater 10s Antelope 10s Ape 10s Armadillo 10s Donkey 10s Baboon 11s Badger 12s Barracuda 13s Bat 14s Bear 15s Beaver 16s Bee 17s Bison 18s Buffalo 19s
Copyright (c) 2011 Max Kueng (http://maxkueng.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.