{"_id":"deferred-queue","_rev":"135-bad04a47e102814c1f05caff4cfa5dd7","name":"deferred-queue","description":"Series control flow library","dist-tags":{"latest":"0.5.1"},"versions":{"0.0.1":{"name":"deferred-queue","version":"0.0.1","description":"Asynchronous deferred queue","keywords":["asynchronous","deferred","queue","flow"],"author":{"name":"Gabriel Llamas","email":"gaglekas@gmail.com"},"repository":{"type":"git","url":"git://github.com/Gagle/Node-DeferredQueue.git"},"engines":{"node":"*"},"licenses":[{"type":"MIT","url":"http://www.opensource.org/licenses/mit-license.html"}],"main":"lib/deferred-queue","_id":"deferred-queue@0.0.1","dist":{"shasum":"1b16607c0f17c1ec791f04a5241a0965dab90278","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.0.1.tgz","integrity":"sha512-/1u37g7OMo2yztbQb5qVbwREEgOGPcRIJnmPw8EMaMXcptNj/PLhdzEjKiacCpUrS8wHcjnMqdzEZK0YJdb/0Q==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIB10gDjzJAB+hzj4CmJ+4S4xz3C0r5QWAYja/0jkAsC4AiAqq7TVpLWGuF2HcTu2x/vNORf5L7d9rqKzS2OEMIbmUg=="}]},"_from":".","_npmVersion":"1.2.14","_npmUser":{"name":"Gagle","email":"gaglekas@gmail.com"},"maintainers":[{"name":"Gagle","email":"gaglekas@gmail.com"}],"directories":{}},"0.0.2":{"name":"deferred-queue","version":"0.0.2","description":"Asynchronous deferred queue","keywords":["asynchronous","deferred","queue","flow"],"author":{"name":"Gabriel Llamas","email":"gaglekas@gmail.com"},"repository":{"type":"git","url":"git://github.com/Gagle/Node-DeferredQueue.git"},"engines":{"node":"*"},"licenses":[{"type":"MIT","url":"http://www.opensource.org/licenses/mit-license.html"}],"main":"lib/deferred-queue","_id":"deferred-queue@0.0.2","dist":{"shasum":"c32e81d5e11b3ab94fd61f2e5d28ab4bb25fed1a","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.0.2.tgz","integrity":"sha512-zWWJlZwk+QpMM4sI2iLdcZNuQOUryB8Cb43DeO8MlUyE+7pSenujKRdSrGAzAKdVlwlk6DWt1dl0+zzDxUyPUA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC9qwADCP129bkVNm1nHsVfVbwvPlcA1IGNMyjN393ZKwIgW9S5RdWXpQ1QKzSHTqIPxqdzvGjSVxlYD+sMEmC83WM="}]},"_from":".","_npmVersion":"1.2.18","_npmUser":{"name":"Gagle","email":"gaglekas@gmail.com"},"maintainers":[{"name":"Gagle","email":"gaglekas@gmail.com"}],"directories":{}},"0.0.3":{"name":"deferred-queue","version":"0.0.3","description":"Asynchronous deferred queue","keywords":["asynchronous","deferred","queue","flow","promises"],"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"engines":{"node":">=0.10"},"license":"MIT","main":"lib","bugs":{"url":"https://github.com/gagle/node-deferred-queue/issues"},"_id":"deferred-queue@0.0.3","dist":{"shasum":"947afe60a39a466cf66d418b51f5ac5c47aa98ba","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.0.3.tgz","integrity":"sha512-NsZakSgebCEyUhSelKCnVmHmMCScgs8el03wmxN42HKFmzbyamOCd22kOFQcbZievQ4TwIy0clr2EII5W1M8nA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDK+NtqFYb+cZVbKTkJWE9+1ftqXLX+89at9lYk5ZIPxQIgGhjzhD/+bN0FPDoe7FkMy7t+mtG2I7Fkw77qsVHVhHo="}]},"_from":".","_npmVersion":"1.3.5","_npmUser":{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"},"maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}],"directories":{}},"0.1.0":{"name":"deferred-queue","version":"0.1.0","description":"Asynchronous deferred queue","keywords":["asynchronous","deferred","queue","flow","promises","task"],"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"engines":{"node":">=0.10"},"license":"MIT","main":"lib","bugs":{"url":"https://github.com/gagle/node-deferred-queue/issues"},"_id":"deferred-queue@0.1.0","dist":{"shasum":"1a39ef5e9b026b833621d040211611cc785979b0","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.1.0.tgz","integrity":"sha512-V0fdQWMzfSZix6UtmI4uuCpjrjPY5AMaKYF6aW+9U7CbLIPTCZjN7UPnGUyK+/EnUkKGgqNK8im2SKl+rpRN3w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDxDrt1M9prkqe/74PDgsZel5VhmHTH8CTvSoV+3G4cwgIhAOVaXtSGKFlE3kyqe4U1HP/+E6oqMx3NlMxlqeKcwIq+"}]},"_from":".","_npmVersion":"1.3.5","_npmUser":{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"},"maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}],"directories":{}},"0.1.1":{"name":"deferred-queue","version":"0.1.1","description":"Asynchronous deferred queue","keywords":["asynchronous","deferred","queue","flow","promises","task"],"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"engines":{"node":">=0.10"},"license":"MIT","main":"lib","bugs":{"url":"https://github.com/gagle/node-deferred-queue/issues"},"_id":"deferred-queue@0.1.1","dist":{"shasum":"6a5347e363373bcf47ee178649b19ef5f3cb2e8c","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.1.1.tgz","integrity":"sha512-Pr1PnrT9ZVUE+/m/3X+dKYR6B459MQw6g0WEIgKKBpgV4vfynCzDvEZ/CRTVi6pZdMg5FWhMIzhHgqAi3fJazw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIF1DzMjV03/u+XEigkLY6QcNd1MAsWT2fmpr7TKBCvPcAiEAsjniEFiPtlJJF9Use4nr+QiWCymbKr2igFaTzbThTZA="}]},"_from":".","_npmVersion":"1.3.8","_npmUser":{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"},"maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}],"directories":{}},"0.2.0":{"name":"deferred-queue","version":"0.2.0","description":"Asynchronous deferred queue","keywords":["asynchronous","deferred","queue","flow","task"],"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"engines":{"node":">=0.10"},"license":"MIT","main":"lib","bugs":{"url":"https://github.com/gagle/node-deferred-queue/issues"},"_id":"deferred-queue@0.2.0","dist":{"shasum":"0ac528df94c1a5ede48edafed69539926e37ba6d","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.2.0.tgz","integrity":"sha512-DhfmlbBElBDytqJlwDw0lNnxuLpvRZl6Bo+IBaPNRMyXMh4K+UscEsQClBZRTrqF48AmzLJRM+WfghsIANQg6w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFFqbjJpXGu1IcXsA3TBS58P/aQiSaTjJS/J9PmhhXLsAiBiu00duQ/AnhrxC9FvAJ0xNF27rHbes87266RAd0tS4g=="}]},"_from":".","_npmVersion":"1.3.11","_npmUser":{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"},"maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}],"directories":{}},"0.3.0":{"name":"deferred-queue","version":"0.3.0","description":"Asynchronous deferred queue","keywords":["asynchronous","deferred","queue","flow","task"],"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"engines":{"node":">=0.10"},"license":"MIT","main":"lib","bugs":{"url":"https://github.com/gagle/node-deferred-queue/issues"},"_id":"deferred-queue@0.3.0","dist":{"shasum":"abdcc5ca028d52a65a73c18b01721ebda47a3eaa","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.3.0.tgz","integrity":"sha512-tTcMwu9hheEwbt6sXdUbf15oePfDM9Q8nXA59PoTXVEqFXjaIJH/oeKCo5DDdlg7PCSubCOzdCUUFSHM0VCUIQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIA2F1eM4KF9GnksXC3mgA1Dpp/GaD9cd/mVgYeUzWGPpAiBe8xa1oCK8Qn8p4HbCqHu8KWtmkxuwXG5MKolnDOLuOw=="}]},"_from":".","_npmVersion":"1.3.11","_npmUser":{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"},"maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}],"directories":{}},"0.4.0":{"name":"deferred-queue","version":"0.4.0","description":"Fastest series control flow library","keywords":["asynchronous","deferred","queue","flow","task"],"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"engines":{"node":">=0.10"},"devDependencies":{"async":"*","speedy":"*"},"license":"MIT","main":"lib","bugs":{"url":"https://github.com/gagle/node-deferred-queue/issues"},"_id":"deferred-queue@0.4.0","dist":{"shasum":"bddabf145bbccfe2256081e7c1c5509d3057eaf2","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.4.0.tgz","integrity":"sha512-OYCREPipj1ZSK4fFNe9tMBgI5lYqkpr2Fn+NtzhSZ4SUV22DFXDjjYvBHkdxJ/2KuqUUIvfYoCPGiY3mofSuVg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEkcCbHrRJPjuBuZGOjzcJFC3YFW5xmhLItodGSJ2mLTAiEAicfBkSS8kHR9Hm2Ieu7B+/79KB+X7PStstriZW68mxQ="}]},"_from":".","_npmVersion":"1.3.11","_npmUser":{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"},"maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}],"directories":{}},"0.5.0":{"name":"deferred-queue","version":"0.5.0","description":"Series control flow library","keywords":["asynchronous","deferred","queue","flow","task"],"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"engines":{"node":">=0.10"},"devDependencies":{"async":"0.2.x","q":"0.9.x","speedy":"*","step":"0.0.x"},"license":"MIT","main":"lib","bugs":{"url":"https://github.com/gagle/node-deferred-queue/issues"},"_id":"deferred-queue@0.5.0","dist":{"shasum":"30f6b84bf216aa20421918a61ca69387061fc935","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.5.0.tgz","integrity":"sha512-xPRK+JzamFPeVKRmX1cFiKkLarxv88GhRn+wU10ug7//HxbUjrNB5wXUX/woesKzogZyf///kk8JSOjPxMbYTQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDBtOHgCpRNvOAetzDmRh70Px0vMVi5dJrbxb1AH8LI2AiAN46LZRQ73MRyy4A/rkCCVqgkW1nVVfcDoIUbRjiNZtw=="}]},"_from":".","_npmVersion":"1.3.11","_npmUser":{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"},"maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}],"directories":{}},"0.5.1":{"name":"deferred-queue","version":"0.5.1","description":"Series control flow library","keywords":["asynchronous","deferred","queue","flow","task"],"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"engines":{"node":">=0.10"},"devDependencies":{"async":"0.2.x","bluebird":"0.8.x","q":"0.9.x","speedy":"*","step":"0.0.x"},"scripts":{"test":"node test/index"},"license":"MIT","main":"lib","bugs":{"url":"https://github.com/gagle/node-deferred-queue/issues"},"homepage":"https://github.com/gagle/node-deferred-queue","_id":"deferred-queue@0.5.1","dist":{"shasum":"c0a93a9dfe711fe3d8ca247740cf48ec11175ac7","tarball":"https://registry.npmjs.org/deferred-queue/-/deferred-queue-0.5.1.tgz","integrity":"sha512-p1QxX8DoWoUpFVnq/Ig0K8u4VbcLzGuUeuNj1OSDwqkBR+u9vIqzAffvTVCKzLXY7rAaiUEnEpIqycC5hiKX4w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCAM1bxUyXibyhzJ0fD8O1dYW4VMZXMUU1gQIuyq3i3xQIhANlRkYrHLeCK9u8Wzg9ZC/s0eh6UWHJd36exR+plNRcr"}]},"_from":".","_npmVersion":"1.3.24","_npmUser":{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"},"maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}]}},"readme":"deferred-queue\r\n==============\r\n\r\n#### Series control flow library ####\r\n\r\n[![NPM version](https://badge.fury.io/js/deferred-queue.png)](http://badge.fury.io/js/deferred-queue \"Fury Version Badge\")\r\n[![Build Status](https://secure.travis-ci.org/gagle/node-deferred-queue.png)](http://travis-ci.org/gagle/node-deferred-queue \"Travis CI Badge\")\r\n\r\n[![NPM installation](https://nodei.co/npm/deferred-queue.png?mini=true)](https://nodei.co/npm/deferred-queue \"NodeICO Badge\")\r\n\r\nThis module brings to you a very lighweight control flow mechanism that it's meant to be the glue between the user calls and the asynchronous nature of your module. It provides a fluent interface, so if your module has an asynchronous API which tends to create the callback pyramid of doom, a deferred queue may help you. It can be also used as a standalone module.\r\n\r\nFor example, suppose you have an API like the following one:\r\n\r\n```javascript\r\nvar r = new Reader (\"file\");\r\n\r\nr.read (10, function (error, bytesRead, buffer){\r\n  if (error) return console.error (error);\r\n  fn1 (bytesRead, buffer);\r\n  \r\n  r.read (20, function (error, bytesRead, buffer){\r\n    if (error) return console.error (error);\r\n    fn2 (bytesRead, buffer);\r\n    \r\n    r.close (function (error){\r\n      if (error) return console.error (error);\r\n      fn3 ();\r\n    });\r\n  });\r\n});\r\n```\r\n\r\nThe above example has two problems: the callback nesting and the error handling. With a deferred queue the example can be rewritten as follows:\r\n\r\n```javascript\r\nvar r = new Reader (\"file\");\r\n\r\nr.on (\"error\", function (error){\r\n\tconsole.error (error);\r\n});\r\nr.on (\"close\", fn3);\r\nr.read (10, fn1);\r\nr.read (20, fn2);\r\nr.close ();\r\n```\r\n\r\nLook at the [reader](https://github.com/gagle/node-deferred-queue/blob/master/examples/reader.js) example for further details.\r\n\r\n__Projects using this library:__\r\n\r\n- [binary-reader](https://github.com/gagle/node-binary-reader): A real project based on the previous example.\r\n- [seraphim](https://github.com/gagle/node-seraphim): Configuration loading made easy.\r\n\r\n#### Documentation ####\r\n\r\n- [async vs deferred-queue](#async-dq)\r\n\r\n#### Functions ####\r\n\r\n- [_module_() : DeferredQueue](#create)\r\n\r\n#### Objects ####\r\n\r\n- [DeferredQueue](#deferredqueue)\r\n\r\n---\r\n\r\n<a name=\"async-dq\"></a>\r\n__async vs deferred-queue__\r\n\r\n`async.waterfall()` is the function with more similarities with a deferred queue.\r\n\r\n```javascript\r\nasync.waterfall ([\r\n  //Task 1\r\n  function (cb){\r\n    cb (null, 1, 2);\r\n  },\r\n  //Task 2\r\n  function (n1, n2, cb){\r\n    //n1 is 1, n2 is 2\r\n    cb ();\r\n  }\r\n], function (error){\r\n  if (error) return console.error (error);\r\n});\r\n```\r\n\r\n```javascript\r\ndq ()\r\n    .on (\"error\", function (error){\r\n      console.error (error);\r\n    })\r\n    //Task 1\r\n    .push (function (cb){\r\n      cb (null, 1, 2);\r\n    }, function (error, n1, n2){\r\n      //n1 is 1, n2 is 2\r\n    })\r\n    //Task 2\r\n    .push (function (){});\r\n```\r\n\r\nBoth are very similar but there are 3 big differences:\r\n\r\n- `async`'s error handling has a major flaw. When something fails you don't know from which task comes the error, so you cannot apply rollback or fallback strategies.\r\n\r\n  This library separates a task from its result. If you look at the task 1 you can see that [push()](#push) receives a second function as a parameter, it's the result of the task and is executed between two tasks: the current and the next. If the current task fails, the error is passed to this function and then the `error` event is emitted (if [preventDefault()]() is not called).\r\n\r\n- `async.waterfall()` forwards the result values to the next task. That's ok until you need to use these values from any other task. Javascript has closures, let's use them. There's no need to pass the values to the next task, simply store them in a closure (the second parameter of [push()](#push)) and let the user decide where to save them.\r\n\r\n    ```javascript\r\n    var myValue;\r\n    \r\n    dq ()\r\n        .on (\"error\", function (error){\r\n          console.error (error);\r\n        })\r\n        .push (function (cb){\r\n          cb (null, 1);\r\n        }, function (error, n){\r\n          if (error) return;\r\n          myValue = n;\r\n        })\r\n        .push (function (){})\r\n        .push (function (){\r\n          //Use myValue\r\n        });\r\n    ```\r\n\r\n- `async` internally uses `process.nextTick()` to call the next task. On the other hand, `deferred-queue` doesn't make any assumption, you decide how to enqueue the tasks; synchronously, asynchronously or both.\r\n\r\n    ```javascript\r\n    dq ()\r\n        .on (\"error\", function (error){\r\n          console.error (error);\r\n        })\r\n        //Synchronous\r\n        .push (function (){})\r\n        //Asynchronous\r\n        .push (function (cb){\r\n          process.nextTick (cb);\r\n        })\r\n        //Fake asynchronous (synchronous)\r\n        .push (function (cb){\r\n          cb ();\r\n        });\r\n    ```\r\n\r\n---\r\n\r\n<a name=\"create\"></a>\r\n___module_() : DeferredQueue__\r\n\r\nReturns a new [DeferredQueue](#deferredqueue) instance.\r\n\r\n```javascript\r\nvar dq = require (\"deferred-queue\");\r\nvar q = dq ();\r\n```\r\n\r\n---\r\n\r\n<a name=\"deferredqueue\"></a>\r\n__DeferredQueue__\r\n\r\n__Events__\r\n\r\n- [error](#event_error)\r\n\r\n__Methods__\r\n\r\n- [DeferredQueue#pause() : undefined](#pause)\r\n- [DeferredQueue#pending() : Number](#pending)\r\n- [DeferredQueue#preventDefault() : undefined](#preventDefault)\r\n- [DeferredQueue#push(task[, result]) : DeferredQueue](#push)\r\n- [DeferredQueue#resume() : undefined](#resume)\r\n- [DeferredQueue#unshift(task[, result]) : DeferredQueue](#unshift)\r\n\r\n<a name=\"event_error\"></a>\r\n__Error__\r\n\r\nArguments: `error`.\r\n\r\nEmitted when an error occurs.\r\n\r\n---\r\n\r\n<a name=\"pause\"></a>\r\n__DeferredQueue#pause() : undefined__\r\n\r\nPauses the queue execution. Look at the [async-function-between-tasks.js](https://github.com/gagle/node-deferred-queue/blob/master/examples/async-function-between-tasks.js) example for further details.\r\n\r\n```javascript\r\nq\r\n    .push (function (){\r\n    \t//Task\r\n    }, function (){\r\n    \t//Callback\r\n    \tthis.pause ();\r\n    })\r\n    .push (function (){\r\n    \t//This task is not executed until you call to \"resume()\"\r\n    });\r\n```\r\n\r\n---\r\n\r\n<a name=\"pending\"></a>\r\n__DeferredQueue#pending() : Number__\r\n\r\nReturns the number of pending tasks in the queue.\r\n\r\n---\r\n\r\n<a name=\"preventDefault\"></a>\r\n__DeferredQueue#preventDefault() : undefined__\r\n\r\nPrevents the propagation of the error, that is, the `error` event is not emitted. It must be used from inside the callback parameter of the [push()](#push) and [unshift()](#unshift) functions.\r\n\r\n```javascript\r\nq.push (function (){\r\n\t//Task\r\n\tthrow new Error ();\r\n}, function (error){\r\n\t//Callback\r\n\tif (error){\r\n\t\tthis.preventDefault ();\r\n\t}\r\n});\r\n```\r\n\r\n---\r\n\r\n<a name=\"push\"></a>\r\n__DeferredQueue#push(task[, result]) : DeferredQueue__\r\n\r\nAdds a task to the end of the queue and tries to execute it. If there are pending tasks, it simply waits until all the previous tasks have been executed. Think about it like a queue that is permanently executing tasks. Whenever you add a task it can be immediately executed because the queue is empty or enqueued if there are pending tasks that need to be executed first.\r\n\r\nThe `task` is the function that you want to execute. The `result` is a callback that is executed when the task finishes.\r\n\r\nThe tasks can be synchronous or asynchronous.\r\n\r\n__Synchronous__\r\n\r\nYou can only return one value. If you want to return an error, throw it, it will be catched. Both the error and the value are passed to the result callback, if any.\r\n\r\n```javascript\r\nq.push (function (){\r\n  return 1;\r\n}, function (error, value){\r\n  //error is null\r\n  //value is 1\r\n});\r\n```\r\n```javascript\r\nq.push (function (){\r\n  throw 1;\r\n}, function (error, value){\r\n  //error is 1\r\n  //value is undefined\r\n});\r\n```\r\n\r\n__Asynchronous__\r\n\r\nIf you want to execute an asynchronous task, you must call the `cb` parameter when you are ready to continue. As usual, the error is the first parameter.\r\n\r\n```javascript\r\nq.push (function (cb){\r\n  process.nextTick (function (){\r\n    cb (null, 1, 2);\r\n  });\r\n}, function (error, v1, v2){\r\n  //error is null\r\n  //v1 is 1\r\n  //v2 is 2\r\n});\r\n```\r\n```javascript\r\nq.push (function (cb){\r\n  cb (1);\r\n}, function (error, v1, v2){\r\n  //error is 1\r\n  //v1 and v2 are undefined\r\n});\r\n```\r\n\r\nNote: Being synchronous or asynchronous depends exclusively on the user, but if you use the `cb` parameter, a different internal strategy is used. In other words, you can execute a synchronous task using the `cb` parameter. This is useful when you need to return more than one value.\r\n\r\nThere are subtle differences when the tasks are synchronous or asynchronous:\r\n\r\n```javascript\r\nq.push (A);\r\nq.push (function (){\r\n  q.push (C);\r\n  q.push (D);\r\n});\r\nq.push (B);\r\n```\r\n\r\nIf A, B, C, D are asynchronous: A → B → C → D. [Asynchronous](https://github.com/gagle/node-deferred-queue/blob/master/examples/asynchronous.js) example.  \r\nIf A, B, C, D are synchronous: A → C → D → B. [Synchronous](https://github.com/gagle/node-deferred-queue/blob/master/examples/synchronous.js) example.  \r\n\r\nWhen an error occurs, it is passed to the callback and then, it is emitted. If you don't want to emit the error, call to [preventDefault()](#preventdefault):\r\n\r\n```javascript\r\nq\r\n    .on (\"error\", function (error){\r\n      //This function is not executed\r\n    })\r\n    .push (function (){\r\n      throw new Error ();\r\n    }, function (error){\r\n      if (error) this.preventDefault ();\r\n    });\r\n```\r\n\r\n__I want to execute an asynchronous function inside the result callback__\r\n\r\nYou can. Pause the queue and when you are ready to continue, resume it. Look at the [async-function-between-tasks.js](https://github.com/gagle/node-deferred-queue/blob/master/examples/async-function-between-tasks.js) example for further details.\r\n\r\n---\r\n\r\n<a name=\"resume\"></a>\r\n__DeferredQueue#resume() : undefined__\r\n\r\nResumes the queue execution. Look at the [async-function-between-tasks.js](https://github.com/gagle/node-deferred-queue/blob/master/examples/async-function-between-tasks.js) example for further details.\r\n\r\n---\r\n\r\n<a name=\"unshift\"></a>\r\n__DeferredQueue#unshift(task[, result]) : DeferredQueue__\r\n\r\nAdds a task to the beginning of the queue. It has the same functionality as the [push()](#push) function.","maintainers":[{"name":"gagle","email":"gabriel_llamas_llopis@yahoo.es"}],"time":{"modified":"2022-06-14T23:25:09.425Z","created":"2013-03-13T13:19:24.653Z","0.0.1":"2013-03-13T13:19:29.646Z","0.0.2":"2013-04-23T10:35:04.167Z","0.0.3":"2013-08-10T14:50:05.240Z","0.1.0":"2013-08-12T10:45:01.550Z","0.1.1":"2013-08-31T17:30:55.137Z","0.2.0":"2013-10-04T18:57:37.346Z","0.3.0":"2013-10-08T13:22:32.804Z","0.4.0":"2013-10-11T16:23:38.028Z","0.5.0":"2013-10-14T19:21:58.367Z","0.5.1":"2014-01-09T10:07:26.668Z"},"author":{"name":"Gabriel Llamas","email":"gagle@outlook.com"},"repository":{"type":"git","url":"git://github.com/gagle/node-deferred-queue.git"},"readmeFilename":"README.md"}