From f7550fa07502405d8f579543fbc8b318617e3c24 Mon Sep 17 00:00:00 2001 From: rzcoder Date: Sat, 13 Jun 2015 15:30:30 +0500 Subject: [PATCH 01/23] package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 29e9806..425a051 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "url": "https://github.com/rzcoder/prow.git" }, "keywords": [ + "js", "promise", "flow" ], From 275d79d3f9ce955d6bab1e77b5aa5d2a4c2b4498 Mon Sep 17 00:00:00 2001 From: rzcoder Date: Sat, 13 Jun 2015 15:31:19 +0500 Subject: [PATCH 02/23] package.json --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 97af2e4..038a912 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,14 @@ ## API -###prow.when(deferreds) -###prow.nextTick(task) +### prow.when(deferreds) +### prow.nextTick(task) ### prow.defer(timeout, timelimit) ### prow.waterfall(tasks) ### prow.parallel(tasks, maxThreads) ### prow.queue(tasks) -###prow.retry(task, times) -###prow.times(task, times) +### prow.retry(task, times) +### prow.times(task, times) ## License From 2b340479f7946038a254da7527d73768f253333b Mon Sep 17 00:00:00 2001 From: rzcoder Date: Sat, 13 Jun 2015 15:31:26 +0500 Subject: [PATCH 03/23] package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 425a051..4068388 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.1", + "version": "0.0.2", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { From 754aab1c24a7606630ddc7e911357e7279c7b417 Mon Sep 17 00:00:00 2001 From: rzcoder Date: Sat, 13 Jun 2015 16:13:45 +0500 Subject: [PATCH 04/23] package.json --- gruntfile.js | 2 ++ package.json | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/gruntfile.js b/gruntfile.js index 275035d..7424036 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -58,5 +58,7 @@ module.exports = function (grunt) { grunt.registerTask('default', ['jshint', 'uglify', 'simplemocha']); + grunt.registerTask('test', ['simplemocha']); + grunt.registerTask('w', ['default', 'watch']); }; \ No newline at end of file diff --git a/package.json b/package.json index 4068388..f165cee 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "prow", - "version": "0.0.2", + "version": "0.0.4", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { + "prepublish" : "grunt", "test": "grunt test" }, "repository": { From c40b1fe0f301b8091667eb7bf4bc5e042351e3e6 Mon Sep 17 00:00:00 2001 From: rzcoder Date: Sat, 13 Jun 2015 16:33:49 +0500 Subject: [PATCH 05/23] dist --- dist/prow.js | 14 ++++++++++++-- dist/prow.min.js | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dist/prow.js b/dist/prow.js index bc77964..f921672 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -1,8 +1,8 @@ (function() { var a = {}; a.when = function(b) { - if (b instanceof Promise && typeof b.then === "function") { - return b; + if (b instanceof Promise && typeof result.then === "function") { + return result; } else { var c = a.defer(); c.resolve(b); @@ -130,6 +130,16 @@ f(--c); return d.promise; }; + a.times = function(b, c) { + c = c === undefined ? 1 : c; + var d = []; + var e = a.defer(); + for (var f = 0; f < c; f++) { + d.push(b.call()); + } + Promise.all(d).then(e.resolve.bind(e, d), e.resolve.bind(e, d)); + return e.promise; + }; if (typeof module == "object" && module.exports) { module.exports = a; } else if (typeof define == "function" && define.amd) { diff --git a/dist/prow.min.js b/dist/prow.min.js index 7813aca..332ee61 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){if(b instanceof Promise&&"function"==typeof b.then)return b;var c=a.defer();return c.resolve(b),c.promise},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c){c=void 0===c?1:c;var d=a.defer(),e=function(a){0===c?d.reject(a):f(--c)},f=function(a){b.call().then(function(a){d.resolve(a)},e)["catch"](e)};return f(--c),d.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){if(b instanceof Promise&&"function"==typeof result.then)return result;var c=a.defer();return c.resolve(b),c.promise},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c){c=void 0===c?1:c;var d=a.defer(),e=function(a){0===c?d.reject(a):f(--c)},f=function(a){b.call().then(function(a){d.resolve(a)},e)["catch"](e)};return f(--c),d.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b.call());return Promise.all(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file From 8db12bae1f9e1daabc67840e353f672fdc59b286 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Mon, 15 Jun 2015 13:48:55 +0500 Subject: [PATCH 06/23] fix when --- dist/prow.js | 202 +++++++++++++++++++++++------------------------ dist/prow.min.js | 2 +- gruntfile.js | 1 + package.json | 2 +- src/prow.js | 4 +- 5 files changed, 106 insertions(+), 105 deletions(-) diff --git a/dist/prow.js b/dist/prow.js index f921672..f0cc507 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -1,152 +1,152 @@ (function() { - var a = {}; - a.when = function(b) { - if (b instanceof Promise && typeof result.then === "function") { - return result; + var prow = {}; + prow.when = function(deferreds) { + if (deferreds instanceof Promise && typeof deferreds.then === "function") { + return deferreds; } else { - var c = a.defer(); - c.resolve(b); - return c.promise; + var deferred = prow.defer(); + deferred.resolve(deferreds); + return deferred.promise; } }; - a.nextTick = function(a) { + prow.nextTick = function(task) { if (process && process.nextTick) { - process.nextTick(a); + process.nextTick(task); } else { - setTimeout(a, 0); + setTimeout(task, 0); } }; - a.defer = function(a, b) { - var c = {}; - var d, e; - c.promise = new Promise(function(f, g) { - if (a) { - d = setTimeout(f, a); + prow.defer = function(timeout, timelimit) { + var defer = {}; + var timeoutResolve, timeoutReject; + defer.promise = new Promise(function(resolve, reject) { + if (timeout) { + timeoutResolve = setTimeout(resolve, timeout); } - if (b) { - e = setTimeout(g, b); + if (timelimit) { + timeoutReject = setTimeout(reject, timelimit); } - c.resolve = function() { - clearTimeout(d); - f.apply(this, arguments); + defer.resolve = function() { + clearTimeout(timeoutResolve); + resolve.apply(this, arguments); }; - c.reject = function() { - clearTimeout(e); - g.apply(this, arguments); + defer.reject = function() { + clearTimeout(timeoutReject); + reject.apply(this, arguments); }; }); - return c; + return defer; }; - a.delay = function(a, b) { - var c = new Promise(function(c, d) { + prow.delay = function(timeout, result) { + var promise = new Promise(function(resolve, reject) { setTimeout(function() { - c(b); - }, a); + resolve(result); + }, timeout); }); - return c; + return promise; }; - a.limit = function(a, b) { - var c = new Promise(function(c, d) { + prow.limit = function(timelimit, reason) { + var promise = new Promise(function(resolve, reject) { setTimeout(function() { - d(b); - }, a); + reject(reason); + }, timelimit); }); - return c; + return promise; }; - a.waterfall = function(b) { - var c = b.length; - var d = a.defer(); + prow.waterfall = function(tasks) { + var length = tasks.length; + var deferred = prow.defer(); try { - var e = function(f, g) { - if (f >= c) { - d.resolve(g); + var process = function(cursor, result) { + if (cursor >= length) { + deferred.resolve(result); } else { - var h = b[f]; - a.when(h.call(null, g)).then(function(a) { - e(++f, a); - }, function(a) { - d.reject(a); - }).catch(function(a) { - d.reject(a); + var task = tasks[cursor]; + prow.when(task.call(null, result)).then(function(result) { + process(++cursor, result); + }, function(reason) { + deferred.reject(reason); + }).catch(function(err) { + deferred.reject(err); }); } }; - e(0); - } catch (f) { - d.reject(f); + process(0); + } catch (err) { + deferred.reject(err); } - return d.promise; + return deferred.promise; }; - a.parallel = function(b, c) { - var d = b.length; - var e = a.defer(); - c = Math.min(c || d, d); - var f = 0; - var g = 0; - var h = function() { - if (g >= d) { - if (f === 0) { - e.resolve(); + prow.parallel = function(tasks, maxThreads) { + var length = tasks.length; + var deferred = prow.defer(); + maxThreads = Math.min(maxThreads || length, length); + var inProgress = 0; + var cursor = 0; + var process = function() { + if (cursor >= length) { + if (inProgress === 0) { + deferred.resolve(); } return; } - var i = b[g++]; - f++; - a.when(i.call()).then(function() { - f--; - h(); + var task = tasks[cursor++]; + inProgress++; + prow.when(task.call()).then(function() { + inProgress--; + process(); }, function() { - f--; - h(); + inProgress--; + process(); }).catch(function() { - f--; - h(); + inProgress--; + process(); }); - if (f < c) { - h(); + if (inProgress < maxThreads) { + process(); } }; - h(); - return e.promise; + process(); + return deferred.promise; }; - a.queue = function(b) { - return a.parallel.call(this, b, 1); + prow.queue = function(tasks) { + return prow.parallel.call(this, tasks, 1); }; - a.retry = function(b, c) { - c = c === undefined ? 1 : c; - var d = a.defer(); - var e = function(a) { - if (c === 0) { - d.reject(a); + prow.retry = function(task, times) { + times = times === undefined ? 1 : times; + var deferred = prow.defer(); + var rejHandler = function(reason) { + if (times === 0) { + deferred.reject(reason); } else { - f(--c); + process(--times); } }; - var f = function(a) { - b.call().then(function(a) { - d.resolve(a); - }, e).catch(e); + var process = function(times) { + task.call().then(function(result) { + deferred.resolve(result); + }, rejHandler).catch(rejHandler); }; - f(--c); - return d.promise; + process(--times); + return deferred.promise; }; - a.times = function(b, c) { - c = c === undefined ? 1 : c; - var d = []; - var e = a.defer(); - for (var f = 0; f < c; f++) { - d.push(b.call()); + prow.times = function(task, times) { + times = times === undefined ? 1 : times; + var results = []; + var deferred = prow.defer(); + for (var i = 0; i < times; i++) { + results.push(task.call()); } - Promise.all(d).then(e.resolve.bind(e, d), e.resolve.bind(e, d)); - return e.promise; + Promise.all(results).then(deferred.resolve.bind(deferred, results), deferred.resolve.bind(deferred, results)); + return deferred.promise; }; if (typeof module == "object" && module.exports) { - module.exports = a; + module.exports = prow; } else if (typeof define == "function" && define.amd) { define(function() { - return a; + return prow; }); } else if (typeof window == "object") { - window.prow = a; + window.prow = prow; } })(); \ No newline at end of file diff --git a/dist/prow.min.js b/dist/prow.min.js index 332ee61..f6c2b60 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){if(b instanceof Promise&&"function"==typeof result.then)return result;var c=a.defer();return c.resolve(b),c.promise},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c){c=void 0===c?1:c;var d=a.defer(),e=function(a){0===c?d.reject(a):f(--c)},f=function(a){b.call().then(function(a){d.resolve(a)},e)["catch"](e)};return f(--c),d.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b.call());return Promise.all(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){if(b instanceof Promise&&"function"==typeof b.then)return b;var c=a.defer();return c.resolve(b),c.promise},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c){c=void 0===c?1:c;var d=a.defer(),e=function(a){0===c?d.reject(a):f(--c)},f=function(a){b.call().then(function(a){d.resolve(a)},e)["catch"](e)};return f(--c),d.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b.call());return Promise.all(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/gruntfile.js b/gruntfile.js index 7424036..d6b7827 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -17,6 +17,7 @@ module.exports = function (grunt) { }, js: { options: { + mangle: false, compress: false, beautify: true }, diff --git a/package.json b/package.json index f165cee..30f19ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.4", + "version": "0.0.5", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index 4f89b3e..a900d47 100644 --- a/src/prow.js +++ b/src/prow.js @@ -7,8 +7,8 @@ * @returns {Promise} */ prow.when = function (deferreds) { - if (deferreds instanceof Promise && typeof result.then === "function") { - return result; + if (deferreds instanceof Promise && typeof deferreds.then === "function") { + return deferreds; } else { var deferred = prow.defer(); deferred.resolve(deferreds); From 2409fc14a21abd0984e76aa6446667a1b2c986f8 Mon Sep 17 00:00:00 2001 From: rzcoder Date: Mon, 15 Jun 2015 23:36:56 +0500 Subject: [PATCH 07/23] fix prow.when --- dist/prow.js | 19 +++++++++++++++---- dist/prow.min.js | 2 +- package.json | 2 +- src/prow.js | 19 +++++++++++++++---- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/dist/prow.js b/dist/prow.js index f0cc507..6cffae5 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -1,10 +1,21 @@ (function() { var prow = {}; prow.when = function(deferreds) { + var deferred; if (deferreds instanceof Promise && typeof deferreds.then === "function") { - return deferreds; + if (deferreds instanceof Promise) { + return deferreds; + } else { + deferred = prow.defer(); + deferreds.then(function() { + deferred.resolve.apply(this, arguments); + }, function() { + deferred.reject.apply(this, arguments); + }); + return deferred.promise; + } } else { - var deferred = prow.defer(); + deferred = prow.defer(); deferred.resolve(deferreds); return deferred.promise; } @@ -123,7 +134,7 @@ } }; var process = function(times) { - task.call().then(function(result) { + prow.when(task.call()).then(function(result) { deferred.resolve(result); }, rejHandler).catch(rejHandler); }; @@ -135,7 +146,7 @@ var results = []; var deferred = prow.defer(); for (var i = 0; i < times; i++) { - results.push(task.call()); + results.push(prow.when(task.call())); } Promise.all(results).then(deferred.resolve.bind(deferred, results), deferred.resolve.bind(deferred, results)); return deferred.promise; diff --git a/dist/prow.min.js b/dist/prow.min.js index f6c2b60..4f99503 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){if(b instanceof Promise&&"function"==typeof b.then)return b;var c=a.defer();return c.resolve(b),c.promise},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c){c=void 0===c?1:c;var d=a.defer(),e=function(a){0===c?d.reject(a):f(--c)},f=function(a){b.call().then(function(a){d.resolve(a)},e)["catch"](e)};return f(--c),d.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b.call());return Promise.all(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):(c=a.defer(),c.resolve(b),c.promise)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c){c=void 0===c?1:c;var d=a.defer(),e=function(a){0===c?d.reject(a):f(--c)},f=function(c){a.when(b.call()).then(function(a){d.resolve(a)},e)["catch"](e)};return f(--c),d.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(a.when(b.call()));return Promise.all(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/package.json b/package.json index 30f19ce..61fadff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.5", + "version": "0.0.6", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index a900d47..262178e 100644 --- a/src/prow.js +++ b/src/prow.js @@ -7,10 +7,21 @@ * @returns {Promise} */ prow.when = function (deferreds) { + var deferred; if (deferreds instanceof Promise && typeof deferreds.then === "function") { - return deferreds; + if (deferreds instanceof Promise) { + return deferreds; + } else { + deferred = prow.defer(); + deferreds.then(function () { + deferred.resolve.apply(this, arguments); + }, function () { + deferred.reject.apply(this, arguments); + }); + return deferred.promise; + } } else { - var deferred = prow.defer(); + deferred = prow.defer(); deferred.resolve(deferreds); return deferred.promise; } @@ -194,7 +205,7 @@ }; var process = function (times) { - task.call().then(function (result) { + prow.when(task.call()).then(function (result) { deferred.resolve(result); }, rejHandler).catch(rejHandler); }; @@ -214,7 +225,7 @@ var results = []; var deferred = prow.defer(); for (var i = 0; i < times; i++) { - results.push(task.call()); + results.push(prow.when(task.call())); } Promise.all(results).then(deferred.resolve.bind(deferred, results), deferred.resolve.bind(deferred, results)); return deferred.promise; From 9667e807f3e92305b971d51e1ad4a64aa40adf01 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Tue, 16 Jun 2015 17:00:14 +0500 Subject: [PATCH 08/23] add delay arg to retry --- package.json | 2 +- src/prow.js | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 61fadff..f2aac78 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.6", + "version": "0.0.7", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index 262178e..be9cd15 100644 --- a/src/prow.js +++ b/src/prow.js @@ -191,16 +191,21 @@ * Attempts to get a successful response from `task` no more than `times` times before returning an error. * @param task {function} Function which return promise * @param times {int} Number of try times, before reject + * @param delay {int} Delay in ms between tries * @returns {Promise} Promise which resolve on first successful try, or reject after defined tries */ - prow.retry = function (task, times) { + prow.retry = function (task, times, delay) { times = times === undefined ? 1 : times; var deferred = prow.defer(); var rejHandler = function (reason) { if (times === 0) { deferred.reject(reason); } else { - process(--times); + if (delay !== undefined) { + prow.delay(delay).then(process.bind(this, --times)); + } else { + process(--times); + } } }; @@ -225,9 +230,9 @@ var results = []; var deferred = prow.defer(); for (var i = 0; i < times; i++) { - results.push(prow.when(task.call())); + results.push(task); } - Promise.all(results).then(deferred.resolve.bind(deferred, results), deferred.resolve.bind(deferred, results)); + prow.queue(results).then(deferred.resolve.bind(deferred, results), deferred.resolve.bind(deferred, results)); return deferred.promise; }; From 16ef813368c9d25f9de12ed6651f7e2b69c535a7 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Tue, 16 Jun 2015 17:00:55 +0500 Subject: [PATCH 09/23] update dist --- dist/prow.js | 12 ++++++++---- dist/prow.min.js | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/dist/prow.js b/dist/prow.js index 6cffae5..006e399 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -123,14 +123,18 @@ prow.queue = function(tasks) { return prow.parallel.call(this, tasks, 1); }; - prow.retry = function(task, times) { + prow.retry = function(task, times, delay) { times = times === undefined ? 1 : times; var deferred = prow.defer(); var rejHandler = function(reason) { if (times === 0) { deferred.reject(reason); } else { - process(--times); + if (delay !== undefined) { + prow.delay(delay).then(process.bind(this, --times)); + } else { + process(--times); + } } }; var process = function(times) { @@ -146,9 +150,9 @@ var results = []; var deferred = prow.defer(); for (var i = 0; i < times; i++) { - results.push(prow.when(task.call())); + results.push(task); } - Promise.all(results).then(deferred.resolve.bind(deferred, results), deferred.resolve.bind(deferred, results)); + prow.queue(results).then(deferred.resolve.bind(deferred, results), deferred.resolve.bind(deferred, results)); return deferred.promise; }; if (typeof module == "object" && module.exports) { diff --git a/dist/prow.min.js b/dist/prow.min.js index 4f99503..ac8a8c5 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):(c=a.defer(),c.resolve(b),c.promise)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c){c=void 0===c?1:c;var d=a.defer(),e=function(a){0===c?d.reject(a):f(--c)},f=function(c){a.when(b.call()).then(function(a){d.resolve(a)},e)["catch"](e)};return f(--c),d.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(a.when(b.call()));return Promise.all(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):(c=a.defer(),c.resolve(b),c.promise)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file From 5e9ff939cd7dc02a58401b9398fb6d7d0aebe692 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Wed, 17 Jun 2015 19:00:42 +0500 Subject: [PATCH 10/23] fix .when --- README.md | 2 +- dist/prow.js | 4 +--- dist/prow.min.js | 2 +- package.json | 2 +- src/prow.js | 4 +--- 5 files changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 038a912..9ac6a96 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ ### prow.waterfall(tasks) ### prow.parallel(tasks, maxThreads) ### prow.queue(tasks) -### prow.retry(task, times) +### prow.retry(task, times, delay) ### prow.times(task, times) ## License diff --git a/dist/prow.js b/dist/prow.js index 006e399..73a28db 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -15,9 +15,7 @@ return deferred.promise; } } else { - deferred = prow.defer(); - deferred.resolve(deferreds); - return deferred.promise; + return Promise.resolve(deferreds); } }; prow.nextTick = function(task) { diff --git a/dist/prow.min.js b/dist/prow.min.js index ac8a8c5..f8d1c1a 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):(c=a.defer(),c.resolve(b),c.promise)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/package.json b/package.json index f2aac78..15299d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.7", + "version": "0.0.8", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index be9cd15..2c28edb 100644 --- a/src/prow.js +++ b/src/prow.js @@ -21,9 +21,7 @@ return deferred.promise; } } else { - deferred = prow.defer(); - deferred.resolve(deferreds); - return deferred.promise; + return Promise.resolve(deferreds); } }; From 6fe2213c75ab11411ae79d26fa878696e66aa268 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Mon, 22 Jun 2015 18:28:18 +0500 Subject: [PATCH 11/23] await --- README.md | 1 + dist/prow.js | 22 ++++++++++++++++++++++ dist/prow.min.js | 2 +- package.json | 2 +- src/prow.js | 31 +++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ac6a96..2cea80c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ ### prow.queue(tasks) ### prow.retry(task, times, delay) ### prow.times(task, times) +### prow.await(condition, checkDelay, timeLimit) ## License diff --git a/dist/prow.js b/dist/prow.js index 73a28db..3a9a8ac 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -153,6 +153,28 @@ prow.queue(results).then(deferred.resolve.bind(deferred, results), deferred.resolve.bind(deferred, results)); return deferred.promise; }; + prow.await = function(condition, checkDelay, timeLimit) { + limit = limit || 0; + var rejected = false; + var timeoutId; + var deferred = prow.defer(null, timeLimit); + var check = function() { + var res = condition(); + if (res) { + deferred.resolve(res); + } else { + if (!rejected) { + timeoutId = setTimeout(check, checkDelay); + } + } + }; + deferred.promise.then(null, function() { + rejected = true; + clearTimeout(timeoutId); + }); + check(); + return deferred.promise; + }; if (typeof module == "object" && module.exports) { module.exports = prow; } else if (typeof define == "function" && define.amd) { diff --git a/dist/prow.min.js b/dist/prow.min.js index f8d1c1a..0fc26de 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){limit=limit||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/package.json b/package.json index 15299d3..f707be5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.8", + "version": "0.0.9", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index 2c28edb..f12e70c 100644 --- a/src/prow.js +++ b/src/prow.js @@ -234,6 +234,37 @@ return deferred.promise; }; + /** + * Awaiting while condition function not return positive bool value. + * @param condition {fucntion} + * @param checkDelay {int} Delay in ms between checks + * @param timeLimit {int} Max time awaiting (0 for infinity) + * @returns {Promise} + */ + prow.await = function (condition, checkDelay, timeLimit) { + limit = limit || 0; + + var rejected = false; + var timeoutId; + var deferred = prow.defer(null, timeLimit); + var check = function() { + var res = condition(); + if (res) { + deferred.resolve(res); + } else { + if (!rejected) { + timeoutId = setTimeout(check, checkDelay); + } + } + }; + deferred.promise.then(null, function() { + rejected = true; + clearTimeout(timeoutId); + }); + check(); + return deferred.promise; + }; + /** * Module loaders */ From 459ba862d35e46e78cb9b6b3ea20c5f0bdd17c6e Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Mon, 22 Jun 2015 18:36:41 +0500 Subject: [PATCH 12/23] fix await --- package.json | 2 +- src/prow.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f707be5..81a0439 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.9", + "version": "0.0.10", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index f12e70c..8eb4a47 100644 --- a/src/prow.js +++ b/src/prow.js @@ -242,7 +242,7 @@ * @returns {Promise} */ prow.await = function (condition, checkDelay, timeLimit) { - limit = limit || 0; + timeLimit = timeLimit || 0; var rejected = false; var timeoutId; From ca13df143b32c6e3f8ba4d732946d392be08d29c Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Tue, 23 Jun 2015 17:19:49 +0500 Subject: [PATCH 13/23] initial control api for parallel --- README.md | 2 +- dist/prow.js | 22 +++++++++++++++++++--- dist/prow.min.js | 2 +- package.json | 2 +- src/prow.js | 24 +++++++++++++++++++++--- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2cea80c..3524281 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ### prow.nextTick(task) ### prow.defer(timeout, timelimit) ### prow.waterfall(tasks) -### prow.parallel(tasks, maxThreads) +### prow.parallel(tasks, maxThreads, managed) ### prow.queue(tasks) ### prow.retry(task, times, delay) ### prow.times(task, times) diff --git a/dist/prow.js b/dist/prow.js index 3a9a8ac..51639c5 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -86,7 +86,7 @@ } return deferred.promise; }; - prow.parallel = function(tasks, maxThreads) { + prow.parallel = function(tasks, maxThreads, managed) { var length = tasks.length; var deferred = prow.defer(); maxThreads = Math.min(maxThreads || length, length); @@ -116,7 +116,23 @@ } }; process(); - return deferred.promise; + if (!managed) { + return deferred.promise; + } else { + return { + addTasks: function(newTasks) { + if (tasks) { + if (Array.isArray(newTasks)) { + tasks = tasks.concat(newTasks); + } else { + tasks.push(newTasks); + } + } + length = tasks.length; + }, + promise: deferred.promise + }; + } }; prow.queue = function(tasks) { return prow.parallel.call(this, tasks, 1); @@ -154,7 +170,7 @@ return deferred.promise; }; prow.await = function(condition, checkDelay, timeLimit) { - limit = limit || 0; + timeLimit = timeLimit || 0; var rejected = false; var timeoutId; var deferred = prow.defer(null, timeLimit); diff --git a/dist/prow.min.js b/dist/prow.min.js index 0fc26de..b06bd38 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c){var d=b.length,e=a.defer();c=Math.min(c||d,d);var f=0,g=0,h=function(){if(g>=d)return void(0===f&&e.resolve());var i=b[g++];f++,a.when(i.call()).then(function(){f--,h()},function(){f--,h()})["catch"](function(){f--,h()}),c>f&&h()};return h(),e.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){limit=limit||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{addTasks:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/package.json b/package.json index 81a0439..2944262 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.10", + "version": "0.0.11", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index 8eb4a47..e5ac606 100644 --- a/src/prow.js +++ b/src/prow.js @@ -135,9 +135,9 @@ * Run the tasks in parallel, without waiting until the previous function has completed. No results passed from promise to promise. * @param tasks {Array} Array of functions which returns promises * @param maxThreads {int} The maximum number of tasks to run at any time. Default: tasks.length - * @returns {Promise} Promise which will resolve after all tasks done (resolved o rejected). + * @returns {Promise|Que Control Api} Promise which will resolve after all tasks done (resolved o rejected) OR Object for controlling tasks que */ - prow.parallel = function (tasks, maxThreads) { + prow.parallel = function (tasks, maxThreads, managed) { var length = tasks.length; var deferred = prow.defer(); maxThreads = Math.min(maxThreads || length, length); @@ -173,7 +173,25 @@ process(); - return deferred.promise; + if (!managed) { + return deferred.promise; + } else { + return { + addTasks: function(newTasks) { + if (tasks) { + if (Array.isArray(newTasks)) { + tasks = tasks.concat(newTasks); + } else { + tasks.push(newTasks); + } + } + + length = tasks.length; + }, + + promise: deferred.promise + }; + } }; /** From fd96e13e613171995f437948c2cb7bb9eae950e2 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Tue, 23 Jun 2015 17:45:39 +0500 Subject: [PATCH 14/23] initial control api for parallel --- README.md | 2 +- dist/prow.js | 6 +++--- dist/prow.min.js | 2 +- package.json | 2 +- src/prow.js | 6 +++--- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3524281..8498814 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ ### prow.defer(timeout, timelimit) ### prow.waterfall(tasks) ### prow.parallel(tasks, maxThreads, managed) -### prow.queue(tasks) +### prow.queue(tasks, managed) ### prow.retry(task, times, delay) ### prow.times(task, times) ### prow.await(condition, checkDelay, timeLimit) diff --git a/dist/prow.js b/dist/prow.js index 51639c5..dc92d14 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -120,7 +120,7 @@ return deferred.promise; } else { return { - addTasks: function(newTasks) { + push: function(newTasks) { if (tasks) { if (Array.isArray(newTasks)) { tasks = tasks.concat(newTasks); @@ -134,8 +134,8 @@ }; } }; - prow.queue = function(tasks) { - return prow.parallel.call(this, tasks, 1); + prow.queue = function(tasks, managed) { + return prow.parallel.call(this, tasks, 1, managed); }; prow.retry = function(task, times, delay) { times = times === undefined ? 1 : times; diff --git a/dist/prow.min.js b/dist/prow.min.js index b06bd38..26e51c0 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{addTasks:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b){return a.parallel.call(this,b,1)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/package.json b/package.json index 2944262..6bb4741 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.11", + "version": "0.0.12", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index e5ac606..c902ccc 100644 --- a/src/prow.js +++ b/src/prow.js @@ -177,7 +177,7 @@ return deferred.promise; } else { return { - addTasks: function(newTasks) { + push: function(newTasks) { if (tasks) { if (Array.isArray(newTasks)) { tasks = tasks.concat(newTasks); @@ -199,8 +199,8 @@ * @param tasks {Array} Array of functions which returns promises * @returns {Promise} Promise which will resolve after all tasks done (resolved o rejected). */ - prow.queue = function (tasks) { - return prow.parallel.call(this, tasks, 1); + prow.queue = function (tasks, managed) { + return prow.parallel.call(this, tasks, 1, managed); }; /** From b649b99dc01313b15c84779849f3ed4215f0d6c5 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Wed, 25 Nov 2015 17:54:04 +0500 Subject: [PATCH 15/23] add timeout error message --- src/prow.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/prow.js b/src/prow.js index c902ccc..d05af33 100644 --- a/src/prow.js +++ b/src/prow.js @@ -14,9 +14,9 @@ } else { deferred = prow.defer(); deferreds.then(function () { - deferred.resolve.apply(this, arguments); + deferred.resolve.apply(this); }, function () { - deferred.reject.apply(this, arguments); + deferred.reject.apply(this); }); return deferred.promise; } @@ -53,12 +53,12 @@ defer.resolve = function () { clearTimeout(timeoutResolve); - resolve.apply(this, arguments); + resolve.apply(this); }; defer.reject = function () { clearTimeout(timeoutReject); - reject.apply(this, arguments); + reject.apply(this, ['PROW TIMEOUT']); }; }); From 39540f59662c93a24443d3b50381a12497c4c15e Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Wed, 25 Nov 2015 17:56:11 +0500 Subject: [PATCH 16/23] add timeout error message --- src/prow.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/prow.js b/src/prow.js index d05af33..48d7dd7 100644 --- a/src/prow.js +++ b/src/prow.js @@ -62,7 +62,6 @@ }; }); - return defer; }; From 534710f0a47561df31888eadb7ef3230029fd340 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Wed, 25 Nov 2015 17:57:31 +0500 Subject: [PATCH 17/23] version bump --- dist/prow.js | 8 ++++---- dist/prow.min.js | 2 +- package.json | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/dist/prow.js b/dist/prow.js index dc92d14..81f5050 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -8,9 +8,9 @@ } else { deferred = prow.defer(); deferreds.then(function() { - deferred.resolve.apply(this, arguments); + deferred.resolve.apply(this); }, function() { - deferred.reject.apply(this, arguments); + deferred.reject.apply(this); }); return deferred.promise; } @@ -37,11 +37,11 @@ } defer.resolve = function() { clearTimeout(timeoutResolve); - resolve.apply(this, arguments); + resolve.apply(this); }; defer.reject = function() { clearTimeout(timeoutReject); - reject.apply(this, arguments); + reject.apply(this, [ "PROW TIMEOUT" ]); }; }); return defer; diff --git a/dist/prow.min.js b/dist/prow.min.js index 26e51c0..4df8b51 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this)},function(){c.reject.apply(this)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this)},e.reject=function(){clearTimeout(d),g.apply(this,["PROW TIMEOUT"])}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/package.json b/package.json index 6bb4741..76ef3e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.12", + "version": "0.0.13", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { From 82201b3de753d89c511ec7aa8eda7c66aa64eed2 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Wed, 25 Nov 2015 17:59:34 +0500 Subject: [PATCH 18/23] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 76ef3e9..6b28455 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.0.13", + "version": "0.1.0", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { From 5f8682b98ec943ce6e3e2c44b19198b975a8d841 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Thu, 26 Nov 2015 16:18:03 +0500 Subject: [PATCH 19/23] fix issue version bump --- dist/prow.js | 6 +++--- dist/prow.min.js | 2 +- package.json | 2 +- src/prow.js | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dist/prow.js b/dist/prow.js index 81f5050..f240bef 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -8,9 +8,9 @@ } else { deferred = prow.defer(); deferreds.then(function() { - deferred.resolve.apply(this); + deferred.resolve.apply(this, arguments); }, function() { - deferred.reject.apply(this); + deferred.reject.apply(this, arguments); }); return deferred.promise; } @@ -37,7 +37,7 @@ } defer.resolve = function() { clearTimeout(timeoutResolve); - resolve.apply(this); + resolve.apply(this, arguments); }; defer.reject = function() { clearTimeout(timeoutReject); diff --git a/dist/prow.min.js b/dist/prow.min.js index 4df8b51..f6b3dda 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this)},function(){c.reject.apply(this)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this)},e.reject=function(){clearTimeout(d),g.apply(this,["PROW TIMEOUT"])}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,["PROW TIMEOUT"])}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/package.json b/package.json index 6b28455..55d45a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.1.0", + "version": "0.2.0", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index 48d7dd7..aad7a90 100644 --- a/src/prow.js +++ b/src/prow.js @@ -14,9 +14,9 @@ } else { deferred = prow.defer(); deferreds.then(function () { - deferred.resolve.apply(this); + deferred.resolve.apply(this, arguments); }, function () { - deferred.reject.apply(this); + deferred.reject.apply(this, arguments); }); return deferred.promise; } @@ -53,7 +53,7 @@ defer.resolve = function () { clearTimeout(timeoutResolve); - resolve.apply(this); + resolve.apply(this, arguments); }; defer.reject = function () { From ffab83358e4ef7410f9f6921c55f0c166eace929 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Wed, 9 Dec 2015 16:35:39 +0500 Subject: [PATCH 20/23] defer reject fix --- package.json | 2 +- src/prow.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 55d45a6..f85ef8c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.2.0", + "version": "0.2.1", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { diff --git a/src/prow.js b/src/prow.js index aad7a90..098b4dc 100644 --- a/src/prow.js +++ b/src/prow.js @@ -48,7 +48,7 @@ timeoutResolve = setTimeout(resolve, timeout); } if (timelimit) { - timeoutReject = setTimeout(reject, timelimit); + timeoutReject = setTimeout(reject.bind(this, 'PROW TIMEOUT'), timelimit); } defer.resolve = function () { @@ -58,7 +58,7 @@ defer.reject = function () { clearTimeout(timeoutReject); - reject.apply(this, ['PROW TIMEOUT']); + reject.apply(this); }; }); From 6293a9adab3f9af6ff0b4b616757593a36eb0e83 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Wed, 9 Dec 2015 16:36:06 +0500 Subject: [PATCH 21/23] defer reject fix --- dist/prow.js | 4 ++-- dist/prow.min.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dist/prow.js b/dist/prow.js index f240bef..b374c42 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -33,7 +33,7 @@ timeoutResolve = setTimeout(resolve, timeout); } if (timelimit) { - timeoutReject = setTimeout(reject, timelimit); + timeoutReject = setTimeout(reject.bind(this, "PROW TIMEOUT"), timelimit); } defer.resolve = function() { clearTimeout(timeoutResolve); @@ -41,7 +41,7 @@ }; defer.reject = function() { clearTimeout(timeoutReject); - reject.apply(this, [ "PROW TIMEOUT" ]); + reject.apply(this); }; }); return defer; diff --git a/dist/prow.min.js b/dist/prow.min.js index f6b3dda..bf801be 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g,b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this,["PROW TIMEOUT"])}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g.bind(this,"PROW TIMEOUT"),b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file From 4b48b80911a6a1d3da10e8b5c86a512086fbcada Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Tue, 12 Jan 2016 16:52:36 +0500 Subject: [PATCH 22/23] defer reject fix --- dist/prow.js | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/prow.js b/dist/prow.js index b374c42..09877ca 100644 --- a/dist/prow.js +++ b/dist/prow.js @@ -35,13 +35,13 @@ if (timelimit) { timeoutReject = setTimeout(reject.bind(this, "PROW TIMEOUT"), timelimit); } - defer.resolve = function() { + defer.resolve = function(result) { clearTimeout(timeoutResolve); resolve.apply(this, arguments); }; - defer.reject = function() { + defer.reject = function(reason) { clearTimeout(timeoutReject); - reject.apply(this); + reject.apply(this, arguments); }; }); return defer; diff --git a/package.json b/package.json index f85ef8c..6d49e39 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prow", - "version": "0.2.1", + "version": "0.2.2", "description": "JS Promises Flow Lib", "main": "dist/prow.js", "scripts": { From 1cfcf9bf2df2e3bf63540bc85e81914b6c631914 Mon Sep 17 00:00:00 2001 From: "s.vychegzhanin" Date: Tue, 12 Jan 2016 16:54:03 +0500 Subject: [PATCH 23/23] defer reject fix --- dist/prow.min.js | 2 +- src/prow.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/prow.min.js b/dist/prow.min.js index bf801be..d8227e3 100644 --- a/dist/prow.min.js +++ b/dist/prow.min.js @@ -1 +1 @@ -!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g.bind(this,"PROW TIMEOUT"),b)),e.resolve=function(){clearTimeout(c),f.apply(this,arguments)},e.reject=function(){clearTimeout(d),g.apply(this)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file +!function(){var a={};a.when=function(b){var c;return b instanceof Promise&&"function"==typeof b.then?b instanceof Promise?b:(c=a.defer(),b.then(function(){c.resolve.apply(this,arguments)},function(){c.reject.apply(this,arguments)}),c.promise):Promise.resolve(b)},a.nextTick=function(a){process&&process.nextTick?process.nextTick(a):setTimeout(a,0)},a.defer=function(a,b){var c,d,e={};return e.promise=new Promise(function(f,g){a&&(c=setTimeout(f,a)),b&&(d=setTimeout(g.bind(this,"PROW TIMEOUT"),b)),e.resolve=function(a){clearTimeout(c),f.apply(this,arguments)},e.reject=function(a){clearTimeout(d),g.apply(this,arguments)}}),e},a.delay=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){c(b)},a)});return c},a.limit=function(a,b){var c=new Promise(function(c,d){setTimeout(function(){d(b)},a)});return c},a.waterfall=function(b){var c=b.length,d=a.defer();try{var e=function(f,g){if(f>=c)d.resolve(g);else{var h=b[f];a.when(h.call(null,g)).then(function(a){e(++f,a)},function(a){d.reject(a)})["catch"](function(a){d.reject(a)})}};e(0)}catch(f){d.reject(f)}return d.promise},a.parallel=function(b,c,d){var e=b.length,f=a.defer();c=Math.min(c||e,e);var g=0,h=0,i=function(){if(h>=e)return void(0===g&&f.resolve());var d=b[h++];g++,a.when(d.call()).then(function(){g--,i()},function(){g--,i()})["catch"](function(){g--,i()}),c>g&&i()};return i(),d?{push:function(a){b&&(Array.isArray(a)?b=b.concat(a):b.push(a)),e=b.length},promise:f.promise}:f.promise},a.queue=function(b,c){return a.parallel.call(this,b,1,c)},a.retry=function(b,c,d){c=void 0===c?1:c;var e=a.defer(),f=function(b){0===c?e.reject(b):void 0!==d?a.delay(d).then(g.bind(this,--c)):g(--c)},g=function(c){a.when(b.call()).then(function(a){e.resolve(a)},f)["catch"](f)};return g(--c),e.promise},a.times=function(b,c){c=void 0===c?1:c;for(var d=[],e=a.defer(),f=0;c>f;f++)d.push(b);return a.queue(d).then(e.resolve.bind(e,d),e.resolve.bind(e,d)),e.promise},a.await=function(b,c,d){d=d||0;var e,f=!1,g=a.defer(null,d),h=function(){var a=b();a?g.resolve(a):f||(e=setTimeout(h,c))};return g.promise.then(null,function(){f=!0,clearTimeout(e)}),h(),g.promise},"object"==typeof module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof window&&(window.prow=a)}(); \ No newline at end of file diff --git a/src/prow.js b/src/prow.js index 098b4dc..3c73174 100644 --- a/src/prow.js +++ b/src/prow.js @@ -51,14 +51,14 @@ timeoutReject = setTimeout(reject.bind(this, 'PROW TIMEOUT'), timelimit); } - defer.resolve = function () { + defer.resolve = function (result) { clearTimeout(timeoutResolve); resolve.apply(this, arguments); }; - defer.reject = function () { + defer.reject = function (reason) { clearTimeout(timeoutReject); - reject.apply(this); + reject.apply(this, arguments); }; });