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