-
Notifications
You must be signed in to change notification settings - Fork 396
/
keys.js
114 lines (96 loc) · 2.86 KB
/
keys.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** @license MIT License (c) copyright 2011-2013 original author or authors */
/**
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
* @author Brian Cavalier
* @author John Hann
*/
(function(define) { 'use strict';
define(function(require) {
var when = require('./when');
var Promise = when.Promise;
var toPromise = when.resolve;
return {
all: when.lift(all),
map: map,
settle: settle
};
/**
* Resolve all the key-value pairs in the supplied object or promise
* for an object.
* @param {Promise|object} object or promise for object whose key-value pairs
* will be resolved
* @returns {Promise} promise for an object with the fully resolved key-value pairs
*/
function all(object) {
var p = Promise._defer();
var resolver = Promise._handler(p);
var results = {};
var keys = Object.keys(object);
var pending = keys.length;
for(var i=0, k; i<keys.length; ++i) {
k = keys[i];
Promise._handler(object[k]).fold(settleKey, k, results, resolver);
}
if(pending === 0) {
resolver.resolve(results);
}
return p;
function settleKey(k, x, resolver) {
/*jshint validthis:true*/
this[k] = x;
if(--pending === 0) {
resolver.resolve(results);
}
}
}
/**
* Map values in the supplied object's keys
* @param {Promise|object} object or promise for object whose key-value pairs
* will be reduced
* @param {function(value:*, key:String):*} f mapping function which may
* return either a promise or a value
* @returns {Promise} promise for an object with the mapped and fully
* resolved key-value pairs
*/
function map(object, f) {
return toPromise(object).then(function(object) {
return all(Object.keys(object).reduce(function(o, k) {
o[k] = toPromise(object[k]).fold(mapWithKey, k);
return o;
}, {}));
});
function mapWithKey(k, x) {
return f(x, k);
}
}
/**
* Resolve all key-value pairs in the supplied object and return a promise
* that will always fulfill with the outcome states of all input promises.
* @param {object} object whose key-value pairs will be settled
* @returns {Promise} promise for an object with the mapped and fully
* settled key-value pairs
*/
function settle(object) {
var keys = Object.keys(object);
var results = {};
if(keys.length === 0) {
return toPromise(results);
}
var p = Promise._defer();
var resolver = Promise._handler(p);
var promises = keys.map(function(k) { return object[k]; });
when.settle(promises).then(function(states) {
populateResults(keys, states, results, resolver);
});
return p;
}
function populateResults(keys, states, results, resolver) {
for(var i=0; i<keys.length; i++) {
results[keys[i]] = states[i];
}
resolver.resolve(results);
}
});
})(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });