Skip to content

Commit f7aa406

Browse files
committed
Migrated async-loops exercise
1 parent 208c437 commit f7aa406

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

exercises/async_loops/exercise.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
"use strict"
22

3-
var input = require('../../input')
4-
var lorem = require('lorem-ipsum')
3+
var deepEqual = require('deep-eql')
4+
var inspect = require('util').inspect
5+
var loremIpsum = require('lorem-ipsum')
6+
var runner = require('../../runner')
57

68
function randomInt(min, max) {
7-
return Math.floor((Math.random() * (max - min + 1)) + min)
9+
return Math.floor((Math.random() * (max - min)) + min)
810
}
911

10-
module.exports = input(new Array(randomInt(10, 20))
11-
.join(',')
12-
.split(',')
13-
.map(function() {
12+
// All deepEqual impls (assert, deep-eql…) seem to b0rk when multiple objects
13+
// in an array share the same `id` value (wtf?!), so we make sure they're unique.
14+
var userCount = randomInt(10, 20)
15+
var userIds = []
16+
while (userIds.length < userCount) {
17+
var id = randomInt(0, 1000)
18+
if (-1 !== userIds.indexOf(id)) continue
19+
userIds.push(id)
20+
}
21+
22+
var users = Array.apply(null, { length: userCount }).map(function() {
1423
return {
15-
id: randomInt(0, 1000),
16-
name: lorem().split(' ').slice(0, 2).map(function(word) {word[0] = word[0].toUpperCase(); return word;}).join(' ')
24+
id: userIds.shift(),
25+
name: loremIpsum().split(' ').slice(0, 2).map(function(word) {
26+
word[0] = word[0].toUpperCase();
27+
return word;
28+
}).join(' ')
1729
}
18-
})).wrap(function(input, mod) {
30+
})
1931

20-
var assert = require('assert')
21-
var inspect = require('util').inspect
32+
var fx
2233

23-
var users = input[0]
24-
var ids = users.map(function(user) {return user.id})
34+
module.exports = runner.custom(function(f) {
35+
fx = f
36+
}).wrapUp(function(callback) {
37+
var self = this
38+
var ids = users.map(function(user) {return user.id })
2539
var load = function(id, fn) {
2640
setTimeout(function() {
2741
var match = users.filter(function(user) {return user.id === id})
@@ -33,13 +47,18 @@ module.exports = input(new Array(randomInt(10, 20))
3347
clearTimeout(tooLong)
3448
console.log(submittedUsers)
3549

36-
assert.deepEqual(submittedUsers, users, 'expected: \n' + inspect(users) + '\n but got: \n'+ inspect(submittedUsers))
50+
if (!deepEqual(submittedUsers, users)) {
51+
self.emit('fail', 'expected: \n' + inspect(users) + '\n but got: \n'+ inspect(submittedUsers))
52+
return callback(null, false)
53+
}
3754

3855
console.log('All %d users loaded!', submittedUsers.length)
56+
callback(null, true)
3957
}
4058

41-
mod.call(mod, ids, load, done)
59+
fx.call(fx, ids, load, done)
4260
var tooLong = setTimeout(function() {
43-
throw new Error('Took too long!')
61+
self.emit('fail', 'Took too long!')
62+
callback(null, false)
4463
}, 1000)
45-
})
64+
}).quiet(users)

0 commit comments

Comments
 (0)