├── minify.sh ├── package.json ├── unblock.min.js ├── README.md └── unblock.js /minify.sh: -------------------------------------------------------------------------------- 1 | uglifyjs -c -m --comments -- unblock.js > unblock.min.js 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "unblock", 3 | "version": "1.0.0", 4 | "description": "a single function for dead simple asynchronous control flow", 5 | "main": "unblock.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/machellerogden/unblock.git" 9 | }, 10 | "keywords": [ 11 | "asynchronous", 12 | "control", 13 | "flow", 14 | "async" 15 | ], 16 | "author": "Mac Heller-Ogden", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/machellerogden/unblock/issues" 20 | }, 21 | "homepage": "https://github.com/machellerogden/unblock" 22 | } 23 | -------------------------------------------------------------------------------- /unblock.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * unblock - a single function for dead simple asynchronous control flow 3 | * 4 | * @version v1.0.0 5 | * @repository https://github.com/machellerogden/unblock 6 | * @author Mac Heller-Ogden 7 | * @copyright Mac Heller-Ogden 2014 8 | * @license MIT 9 | */ 10 | !function(n,t){"undefined"!=typeof module&&module.exports?module.exports=t():"function"==typeof define&&"object"==typeof define.amd?define(t):(function(){return this||(0,eval)("this")}())[n]=t()}("unblock",function(){return function(n){var t=[],e=[],u=[],r={u:function(n){var o=Array.prototype.slice.call(e);return function(){return o.length<1&&(o=arguments.length?arguments:t),setTimeout(function(){t=[n.apply(r,o)],u.length&&u.shift().apply(r,t)},0),r}},then:function(n){return u.push(r.u(n)),r},after:function(n){return function(){return e=arguments,u.push(r.u(n)),r}}};return r.u(n)}}); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unblock.js 2 | 3 | A single function for dead simple asynchronous control flow. Also, this is perhaps the smallest async micro-library ever, weighing in at just 219 bytes gzipped (309 bytes when including module wrapper). 4 | 5 | ## NPM 6 | 7 | npm install unblock 8 | 9 | ## Download 10 | 11 | Download [unblock.js](https://github.com/machellerogden/unblock/blob/master/unblock.js) and include in your page or in your app via script tag or RequireJS. 12 | 13 | 14 | ## Example 15 | 16 | // this is a blocking function to use in the example below 17 | var sleep = function (seconds) { 18 | var ms, startTime; 19 | ms = seconds * 1000; 20 | startTime = new Date().getTime(); // get the current time 21 | while (new Date().getTime() < startTime + ms); // hog cpu 22 | return seconds; 23 | }; 24 | 25 | // this is a simple reporter function to use in the example below 26 | var report = function(x) { console.log(x); }; 27 | 28 | /** 29 | * EXAMPLE 30 | */ 31 | 32 | // this is a basic control flow example 33 | unblock(sleep)(2).then(report).after(sleep)(3).then(report); 34 | 35 | // since the above is unblocked, this will run first 36 | report(1); 37 | 38 | -------------------------------------------------------------------------------- /unblock.js: -------------------------------------------------------------------------------- 1 | /** 2 | * unblock - a single function for dead simple asynchronous control flow 3 | * 4 | * @version v1.0.0 5 | * @repository https://github.com/machellerogden/unblock 6 | * @author Mac Heller-Ogden 7 | * @copyright Mac Heller-Ogden 2014 8 | * @license MIT 9 | */ 10 | 11 | !function (name, definition) { 12 | if (typeof module != 'undefined' && module.exports) module.exports = definition(); 13 | else if (typeof define == 'function' && typeof define.amd == 'object') define(definition); 14 | else (function () { return this || (0, eval)('this'); }())[name] = definition(); 15 | }('unblock', function() { 16 | return function (func) { 17 | var result = [], 18 | args = [], 19 | queue = [], 20 | fn = { 21 | u: function (func) { 22 | var a = Array.prototype.slice.call(args); 23 | return function () { 24 | if (a.length < 1) a = (arguments.length) ? arguments : result; 25 | setTimeout(function () { 26 | result = [func.apply(fn, a)]; 27 | if (queue.length) queue.shift().apply(fn, result); 28 | }, 0); 29 | return fn; 30 | }; 31 | }, 32 | then: function (func) { 33 | queue.push(fn.u(func)); 34 | return fn; 35 | }, 36 | after: function (func) { 37 | return function () { 38 | args = arguments; 39 | queue.push(fn.u(func)); 40 | return fn; 41 | }; 42 | } 43 | }; 44 | return fn.u(func); 45 | }; 46 | }); 47 | --------------------------------------------------------------------------------