Compare commits
	
		
			4 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 178672ad0d | |||
| 9beae76631 | |||
| dd206891d5 | |||
| 038b6cac33 | 
| @ -39,7 +39,7 @@ batchAsync(batchSize, things, doStuff).then(function(results) { | ||||
| }); | ||||
| ``` | ||||
| 
 | ||||
| ## A note on 'undefined' | ||||
| ## A note on `undefined` | ||||
| 
 | ||||
| Returning `undefined` will cause an exception to be thrown, | ||||
| and the Promise to be rejected. | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| (function(exports) { | ||||
| 'use strict'; | ||||
|     "use strict"; | ||||
| 
 | ||||
|     exports.batchAsync = function(limit, arr, doStuff) { | ||||
|         arr = arr.slice(0); | ||||
| @ -37,7 +37,7 @@ exports.batchAsync = function (limit, arr, doStuff) { | ||||
| 
 | ||||
|                 var p; | ||||
|                 try { | ||||
| 				p = doStuff(task); | ||||
|                     p = doStuff(task, index, arr); | ||||
|                 } catch (e) { | ||||
|                     // we need to handle, and bubble, synchronous errors
 | ||||
|                     error = e; | ||||
| @ -48,7 +48,7 @@ exports.batchAsync = function (limit, arr, doStuff) { | ||||
|                 // add support for sync by rapping in a promise
 | ||||
|                 Promise.resolve(p) | ||||
|                     .then(function(result) { | ||||
| 					if ('undefined' === typeof result) { | ||||
|                         if ("undefined" === typeof result) { | ||||
|                             throw new Error( | ||||
|                                 "result was 'undefined'. Please return 'null' to signal that you didn't just forget to return another promise." | ||||
|                             ); | ||||
| @ -67,5 +67,4 @@ exports.batchAsync = function (limit, arr, doStuff) { | ||||
|             doMoreStuff(); | ||||
|         }); | ||||
|     }; | ||||
| 
 | ||||
| }('undefined' !== typeof window ? window : module.exports)); | ||||
| })("undefined" !== typeof window ? window : module.exports); | ||||
|  | ||||
							
								
								
									
										5
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| { | ||||
|   "name": "batchasync", | ||||
|   "version": "1.0.3", | ||||
|   "lockfileVersion": 1 | ||||
| } | ||||
| @ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "batchasync", | ||||
|   "version": "1.0.2", | ||||
|   "version": "1.0.3", | ||||
|   "description": "Like forEachAsync, or Promise.all(), but handling a bounded number of items at any given time.", | ||||
|   "main": "batchasync.js", | ||||
|   "scripts": { | ||||
|  | ||||
							
								
								
									
										53
									
								
								test.js
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								test.js
									
									
									
									
									
								
							| @ -1,15 +1,17 @@ | ||||
| (function(exports) { | ||||
| 'use strict'; | ||||
|     "use strict"; | ||||
| 
 | ||||
| var batchAsync = exports.batchAsync || require('./batchasync.js').batchAsync; | ||||
|     var batchAsync = | ||||
|         exports.batchAsync || require("./batchasync.js").batchAsync; | ||||
| 
 | ||||
|     function testBatch() { | ||||
|         var timeouts = [100, 80, 20, 500, 50, 30, 200, 300]; | ||||
|         console.info(timeouts); | ||||
|         var tasks = timeouts.map(function(timeout, i) { | ||||
|             return function() { | ||||
|                 return promiseTimeout(timeout).then(function() { | ||||
| 				console.log('task:', i, timeouts[i]); | ||||
| 				return i; | ||||
|                     //console.log("task:", i, timeouts[i]);
 | ||||
|                     return i + ":" + timeouts[i]; | ||||
|                 }); | ||||
|             }; | ||||
|         }); | ||||
| @ -19,40 +21,40 @@ function testBatch() { | ||||
|             return task(); | ||||
|         }) | ||||
|             .then(function(results) { | ||||
| 			console.info('results:', results); | ||||
|                 console.info("results:", results); | ||||
|                 if (len !== results.length) { | ||||
| 				throw new Error('result set too small'); | ||||
|                     throw new Error("result set too small"); | ||||
|                 } | ||||
| 			if (results.join(' ') !== results.sort().join(' ')) { | ||||
| 				throw new Error('result set out-of-order'); | ||||
|                 if (results.join(" ") !== results.sort().join(" ")) { | ||||
|                     throw new Error("result set out-of-order"); | ||||
|                 } | ||||
|             }) | ||||
|             .then(function() { | ||||
| 			return batchAsync(4, [], 'not a function').then(function() { | ||||
| 				console.info('Handled ZERO tasks correctly.'); | ||||
|                 return batchAsync(4, [], "not a function").then(function() { | ||||
|                     console.info("Handled ZERO tasks correctly."); | ||||
|                 }); | ||||
|             }) | ||||
|             .then(function() { | ||||
|                 return batchAsync(4, timeouts, function(x) { | ||||
|                     return x; | ||||
|                 }).then(function(results) { | ||||
| 				if (results.join(' ') !== timeouts.join(' ')) { | ||||
|                     if (results.join(" ") !== timeouts.join(" ")) { | ||||
|                         console.error(results); | ||||
| 					throw new Error('sync result set out-of-order'); | ||||
|                         throw new Error("sync result set out-of-order"); | ||||
|                     } | ||||
| 				console.info('Handled sync tasks correctly.'); | ||||
|                     console.info("Handled sync tasks correctly."); | ||||
|                 }); | ||||
|             }) | ||||
|             .then(function() { | ||||
|                 return batchAsync(4, tasks, function(task) { | ||||
|                     if (0 === Math.floor(Math.random() * 2) % 2) { | ||||
| 					throw new Error('any async error will do'); | ||||
|                         throw new Error("any async error will do"); | ||||
|                     } | ||||
|                     return task(); | ||||
|                 }) | ||||
|                     .then(function(results) { | ||||
|                         console.log(results); | ||||
| 					var e = new Error('async rejection should not pass!'); | ||||
|                         var e = new Error("async rejection should not pass!"); | ||||
|                         e.FAIL = true; | ||||
|                         throw e; | ||||
|                     }) | ||||
| @ -60,18 +62,18 @@ function testBatch() { | ||||
|                         if (e.FAIL) { | ||||
|                             throw e; | ||||
|                         } | ||||
| 					console.info('Pass: Exception thrown when expected'); | ||||
|                         console.info("Pass: Exception thrown when expected"); | ||||
|                     }); | ||||
|             }) | ||||
|             .then(function() { | ||||
|                 return batchAsync(4, timeouts, function() { | ||||
|                     if (0 === Math.floor(Math.random() * 2) % 2) { | ||||
| 					throw new Error('any sync error will do'); | ||||
|                         throw new Error("any sync error will do"); | ||||
|                     } | ||||
|                     return null; | ||||
|                 }) | ||||
| 				.then(function(results) { | ||||
| 					var e = new Error('should not pass sync exception!'); | ||||
|                     .then(function(/*results*/) { | ||||
|                         var e = new Error("should not pass sync exception!"); | ||||
|                         e.FAIL = true; | ||||
|                         throw e; | ||||
|                     }) | ||||
| @ -82,29 +84,28 @@ function testBatch() { | ||||
|                     }) | ||||
|                     .then(function() { | ||||
|                         // wait for the tasks the error left dangling to print their message
 | ||||
| 					console.info('Pass: Promise rejected when expected'); | ||||
|                         console.info("Pass: Promise rejected when expected"); | ||||
|                         return promiseTimeout(1000); | ||||
|                     }); | ||||
|             }); | ||||
|     } | ||||
| 
 | ||||
|     function promiseTimeout(timeout) { | ||||
| 	return new Promise(function(resolve, reject) { | ||||
|         return new Promise(function(resolve) { | ||||
|             setTimeout(resolve, timeout); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     testBatch() | ||||
|         .then(function() { | ||||
| 		console.info('PROBABLY PASSED'); | ||||
|             console.info("PROBABLY PASSED"); | ||||
|             console.info( | ||||
| 			'We tested what could be tested without knowing Passed what could be tested Do the results make sense?' | ||||
|                 "We tested what could be tested. Do the results make sense?" | ||||
|             ); | ||||
|         }) | ||||
|         .catch(function(e) { | ||||
| 		console.error('FAIL!'); | ||||
|             console.error("FAIL!"); | ||||
|             console.error(e); | ||||
|             process.exit(500); | ||||
|         }); | ||||
| 
 | ||||
| }('undefined' !== typeof window ? window : module.exports)); | ||||
| })("undefined" !== typeof window ? window : module.exports); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user