-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcodenames-2.js
More file actions
36 lines (31 loc) · 1.32 KB
/
codenames-2.js
File metadata and controls
36 lines (31 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* A simple demonstration of using Pacta to compose asynchronous HTTP requests.
*
* First, see examples/codename.js for background information on what this
* program does and one approach to solve the proble.
*
* This alternate version doesn't use getJSON but actually combines the two
* responses from the web service into an array of strings using conjoin. These
* two values are then parsed ready for use with map or spread.
*/
var http = require('./promised-http');
var random = function (coll) {
return coll[Math.floor(Math.random() * coll.length)];
};
var promisedPrefixesJSON = http.get('http://codenames.clivemurray.com/data/prefixes.json'),
promisedAnimalsJSON = http.get('http://codenames.clivemurray.com/data/animals.json'),
prefixesAndAnimals = promisedPrefixesJSON.
conjoin(promisedAnimalsJSON).
spread(function (prefixes, animals) {
return [JSON.parse(prefixes), JSON.parse(animals)];
});
var promisedCodeName = function () {
return prefixesAndAnimals.spread(function (prefixes, animals) {
var prefix = random(prefixes),
animal = random(animals);
return prefix.title + animal.title;
});
};
promisedCodeName().map(console.log);
promisedCodeName().map(console.log);
promisedCodeName().map(console.log);
promisedCodeName().map(console.log);