-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcodenames.js
More file actions
36 lines (31 loc) · 1.33 KB
/
codenames.js
File metadata and controls
36 lines (31 loc) · 1.33 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.
*
* When run, this program will output four random code names as generated by
* Clive Murray's http://codenames.clivemurray.com.
*
* In this example, http.getJSON() returns a Promise of the parsed JSON from the
* given URL. For these specific URLs, an array of objects will be returned (one
* URL returns a list of "prefixes" and the other returns a list of animal
* names).
*
* See examples/codenames-2.js for another way to achieve the same result.
*/
var http = require('./promised-http'),
Promise = require('../lib/pacta');
var random = function (coll) {
return coll[Math.floor(Math.random() * coll.length)];
};
var promisedPrefixes = http.getJSON('http://codenames.clivemurray.com/data/prefixes.json'),
promisedAnimals = http.getJSON('http://codenames.clivemurray.com/data/animals.json'),
prefixesAndAnimals = Promise.all([promisedPrefixes, promisedAnimals]);
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);