From ec17de65446c88d33556c5d3e315a64cc2fd03c0 Mon Sep 17 00:00:00 2001 From: Christian Adam Date: Thu, 8 Oct 2015 11:14:13 -0300 Subject: [PATCH] v0.0.6 Changes: - It is possible now to get the list of result values. - Introducing "torch" objects that can be passed among functions in the queue. - Possible exceptions of the functions in the queue are now handled and returned as part of rejection value. - Improved documentation adding dependencies state. - Introducing alias all and allSync for resolveAllSync --- AUTHORS | 1 + CHANGELOG | 8 ++ LICENSE | 22 ++++ README.md | 249 +++++++++++++++++++++------------------ demos/PSQueueTest.js | 59 ++++++++++ demos/PSQueueTestFail.js | 67 +++++++++++ demos/QAllTest.js | 56 +++++++++ index.js | 32 ++++- package.json | 15 +-- psqueue.js | 129 ++++++++++++++++---- test/PSQueueTest.js | 27 ----- test/PSQueueTestFail.js | 33 ------ test/QAllTest.js | 27 ----- 13 files changed, 488 insertions(+), 237 deletions(-) create mode 100644 AUTHORS create mode 100644 CHANGELOG create mode 100644 LICENSE create mode 100644 demos/PSQueueTest.js create mode 100644 demos/PSQueueTestFail.js create mode 100644 demos/QAllTest.js delete mode 100644 test/PSQueueTest.js delete mode 100644 test/PSQueueTestFail.js delete mode 100644 test/QAllTest.js diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..bea2ce5 --- /dev/null +++ b/AUTHORS @@ -0,0 +1 @@ +Christian Adam diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..9ecea00 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,8 @@ +v0.0.6: + date: 2015-10-07 + changes: + - It is possible now to get the list of result values. + - Introducing "torch" objects that can be passed among functions in the queue. + - Possible exceptions of the functions in the queue are now handled and returned as part of rejection value. + - Improved documentation adding dependencies state. + - Introducing alias all and allSync for resolveAllSync diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1ea1270 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Christian Adam + +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. + diff --git a/README.md b/README.md index f571242..1a65630 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # PSQueue (Promises Sync Queue) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cgadam/psqueue?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![dependencies](https://david-dm.org/cgadam/psqueue.png)](https://david-dm.org/cgadam/psqueue) ### Getting async ducks in a row. @@ -13,174 +14,196 @@ If this is your case, then this module is for you. ## How to use it? 1. Create a PSQueue. -2. Generate your Q promise. -3. Add the Q promise to the queue (indicating the parameters) +2. Create your function that returns a Q promise. +3. Add the function to the queue (indicating the parameters) 4. Repeat steps 2 and 3 as many times as you want. -5. Call PSQueue's method resolveAllSync() which will return a Q promise too. This returned promise is solved -once ALL the Q promises in the queue are solved. The good part is that the sync queue resolves them in ORDER and -ONE AT THE TIME! :) +5. Call PSQueue's method resolveAllSync() which will return a Q promise too. + +The promise returned by resolveAllSync() will be resolved once ALL the Q promises in the +queue are solved. The good part is that the sync queue resolves all the promises in ORDER: ONE AT THE TIME! :) + +The resolved value the promise returned by resolveAllSync() will contain an array with the resolved values of +all the other synchronously executed promises of the queue. + +You can optionally define a *torch* object and pass it to resolveAllSync function. The object will be systematically passed between the functions in the queue. +Functions can then modify the value and pass it to the following functions in the queue. You need to be sure that your function will actually proccess this parameter. Example: **Q.all** resolves all the promises *asynchronously*. So, in this example: - ```javascript -var Q = require('q'); - -var timestamp = (new Date()).getTime(); -var arr = ['A', 'B', 'C']; - -function doPromiseToItem(item){ - var deferred = Q.defer(); - var randomFactor = 10000 * Math.random() - setTimeout(function(rand){ - console.log(item + ' solved after random ' + (Math.round((rand/1000)*100)/100) + ' secs.'); - deferred.resolve(); - }, randomFactor, randomFactor); - return deferred.promise; -} - -var promises = []; - -for(var i=0; i B,C,A) and the time it took to complete +As you can see *all of the promises ran in parallel* (not in a specific order -> C,B,A) and the time it took to complete all of them is nothing but the time it took for the latest one to finish. **PSQueue** runs all the promises *in order* and *one at the time*: +(Example using a torch value) ```javascript -var Q = require('q'); -var PSQueue = require('psqueue'); - -var timestamp = (new Date()).getTime(); -var arr = ['A', 'B', 'C']; - -function doPromiseToItem(item){ - var deferred = Q.defer(); - var randomFactor = 10000 * Math.random() - setTimeout(function(rand){ - console.log(item + ' solved after random ' + (Math.round((rand/1000)*100)/100) + ' secs.'); - deferred.resolve(); - }, randomFactor, randomFactor); - return deferred.promise; -} - -var promisesQueue = new PSQueue(); - -for(var i=0; i