external-ip
Advanced tools
Comparing version 0.2.2 to 0.2.3
'use strict'; | ||
var requests = require('./requests'); | ||
var asyncLoop = require('evented-async-loop'); | ||
var utils = require('./utils'); | ||
@@ -38,23 +39,22 @@ | ||
var loop = asyncLoop.create(services); | ||
var getIP = { | ||
sequential: function (cb) { | ||
var errors = []; | ||
utils.asyncLoop({ | ||
iterations: services.length, | ||
exec: function (i, stop, next) { | ||
services[i].getIP(function (err, ip) { | ||
if (err) { | ||
err = services[i].url + ' : ' + err; | ||
errors.push(err); | ||
next(); | ||
} else { | ||
stop(ip); | ||
} | ||
}); | ||
}, | ||
done: function (ip) { | ||
cb.apply(null, ip ? [null, ip] : [errors, null]); | ||
} | ||
}); | ||
loop.on('next', function (service, i, arr, errors) { | ||
service.getIP(function (err, ip) { | ||
if (err) { | ||
errors.push(service.url + ' : ' + err); | ||
loop.next(errors); | ||
} else { | ||
loop.done(null, ip); | ||
} | ||
}); | ||
}) | ||
.on('done', function (errors, ip) { | ||
cb.apply(null, ip ? [null, ip] : [errors, null]); | ||
}) | ||
.start([]); | ||
}, | ||
@@ -61,0 +61,0 @@ |
@@ -19,58 +19,2 @@ 'use strict'; | ||
/* | ||
Taken from: http://stackoverflow.com/questions/4288759/asynchronous-for-cycle-in-javascript | ||
Loops through asynchronous code. | ||
@param params {Object} setup the loop by providing the following: | ||
.iterations: {Integer} the number of iterations | ||
.exec: callback that will be executed on each iteration with: | ||
i: the current iteration, | ||
stop: function, stops the loop, | ||
next: function, go to the next iteretion. | ||
.done: callback that will be executed when the loop is done | ||
sample usage: | ||
asyncLoop({ | ||
iterations: 5, | ||
exec: function (i, stop, next) { | ||
console.log(i); | ||
next(); | ||
}, | ||
done: function () { | ||
console.log('done'); | ||
} | ||
}); | ||
*/ | ||
var asyncLoop = function (params) { | ||
var index = 0; | ||
var done = false; | ||
var loop = { | ||
next: function () { | ||
if (done) { | ||
return; | ||
} | ||
if (index < params.iterations) { | ||
index++; | ||
params.exec(loop.iteration(), loop.stop, loop.next); | ||
} else { | ||
done = true; | ||
params.done.apply(null, arguments); | ||
} | ||
}, | ||
iteration: function () { | ||
return index - 1; | ||
}, | ||
stop: function () { | ||
done = true; | ||
params.done.apply(null, arguments); | ||
} | ||
}; | ||
loop.next(); | ||
return loop; | ||
}; | ||
var validateConfig = function (config) { | ||
@@ -121,5 +65,4 @@ return revalidator.validate(config, { | ||
isIP: isIP, | ||
asyncLoop: asyncLoop, | ||
validateConfig: validateConfig, | ||
mergeConfig: mergeConfig | ||
}; |
{ | ||
"name": "external-ip", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "A node.js library to get your external ip from multiple services", | ||
@@ -21,2 +21,3 @@ "main": "index.js", | ||
"network", | ||
"public", | ||
"address" | ||
@@ -38,2 +39,3 @@ ], | ||
"commander": "^2.3.0", | ||
"evented-async-loop": "^0.1.1", | ||
"request": "^2.40.0", | ||
@@ -40,0 +42,0 @@ "revalidator": "^0.2.0" |
@@ -27,40 +27,2 @@ 'use strict'; | ||
it('should loop i times and pass the arguments to done callback', function (cb) { | ||
var i = 10; | ||
utils.asyncLoop({ | ||
iterations: i, | ||
exec: function (i, stop, next) { | ||
next(i, 'someArg'); | ||
}, | ||
done: function (result, anotherArg) { | ||
expect(result).to.equal(i - 1); | ||
expect(anotherArg).to.equal('someArg'); | ||
cb(); | ||
} | ||
}); | ||
}); | ||
it('should loop i times and stop at n and pass the arguments to done callback', function (cb) { | ||
var i = 10; | ||
var n = 4; | ||
utils.asyncLoop({ | ||
iterations: i, | ||
exec: function (i, stop, next) { | ||
if (i === n) { | ||
stop(i, 'stopValue'); | ||
} | ||
next(i, 'someArg'); | ||
}, | ||
done: function (result, anotherArg) { | ||
expect(result).to.equal(n); | ||
expect(anotherArg).to.equal('stopValue'); | ||
cb(); | ||
} | ||
}); | ||
}); | ||
it('should allow valid config', function () { | ||
@@ -67,0 +29,0 @@ var config = { |
21463
4
436
+ Addedevented-async-loop@^0.1.1
+ Addedevented-async-loop@0.1.1(transitive)