Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: Source/core/dom/Promise.js

Issue 16838012: [ABANDONED] Implement Promises. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 (function(postTask) {
32 function append(fulfillCallback, rejectCallback)
33 {
34 if (this.associated.state === 'fulfilled') {
35 postTask(fulfillCallback.bind(undefined, this.associated.result));
abarth-chromium 2013/06/14 05:07:08 We need to be careful about calling things like "b
36 } else if (this.associated.state === 'rejected') {
37 postTask(rejectCallback.bind(undefined, this.associated.result));
38 } else {
39 this.associated.fulfillCallbacks.push(fulfillCallback);
40 this.associated.rejectCallbacks.push(rejectCallback);
41 }
42 }
43
44 /**
45 * Promise.prototype.then API.
46 * @parameter {Function} fulfillCallback
47 * @parameter {Function} rejectCallback
48 */
49 function then(fulfillCallback, rejectCallback)
50 {
51 var resolver;
52 var promise = new Promise(function(r) { resolver = r; });
53 function fulfillWrapper(value)
54 {
55 if (fulfillCallback) {
56 try {
57 value = fulfillCallback.call(promise, value);
58 } catch (e) {
59 resolver.reject(e, true);
60 return;
61 }
62 }
63 resolver.resolve(value, true);
64 }
65
66 function rejectWrapper(value)
67 {
68 if (rejectCallback) {
69 try {
70 value = rejectCallback.call(promise, value);
71 } catch (e) {
72 resolver.reject(e, true);
73 return;
74 }
75 resolver.resolve(value, true);
76 }
77 resolver.reject(value, true);
78 }
79
80 this.append(fulfillWrapper, rejectWrapper);
81 return promise;
82 }
83
84 /**
85 * Promise.prototype.catch API.
86 * @parameter {Function} rejectCallback
87 */
88 function catch_(rejectCallback)
89 {
90 return this.then(undefined, rejectCallback);
91 }
92
93 // static methods
94
95 /**
96 * Promise.fulfill API.
97 * @parameter {?} value the value to be set as the promise result.
98 */
99 function fulfill(value)
100 {
101 var resolver;
102 var promise = new Promise(function(r) { resolver = r; });
103 resolver.fulfill(value);
104 return promise;
105 }
106
107 /**
108 * Promise.resolve API.
109 * @parameter {?} value the value to be set as the promise result.
110 */
111 function resolve(value)
112 {
113 var resolver;
114 var promise = new Promise(function(r) { resolver = r; });
115 resolver.resolve(value);
116 return promise;
117 }
118
119 /**
120 * Promise.reject API.
121 * @parameter {?} value the value to be set as the promise result.
122 */
123 function reject(value)
124 {
125 var resolver;
126 var promise = new Promise(function(r) { resolver = r; });
127 resolver.reject(value);
128 return promise;
129 }
130
131 /**
132 * Promise.every API.
133 * @parameters {...Promise} promises
134 */
135 function every()
136 {
137 var promises = Array.prototype.slice.call(arguments, 0);
abarth-chromium 2013/06/14 05:07:08 Similarly, we don't want to depend on any statics.
138 var resolver;
139 var countdown = promises.length;
140 var results = [];
141 var resultPromise = new Promise(function(r) { resolver = r; });
142
143 if (promises.length === 0) {
144 resolver.resolve(undefined);
145 return resultPromise;
146 }
147 promises.forEach(function(promise, index) {
148 if (!(promise instanceof Promise)) {
149 throw TypeError();
150 }
151 // We use Promise.prototype.then instead of |append| function.
152 promise.then(function(result) {
153 results[index] = result;
154 if (--countdown === 0) {
155 resolver.resolve(results, true);
156 }
157 }, function(result) {
158 resolver.reject(result, true);
159 });
160 });
161 return resultPromise;
162 }
163
164 /**
165 * Promise.any API.
166 * @parameters {...Promise} promises
167 */
168 function any()
169 {
170 var promises = Array.prototype.slice.call(arguments, 0);
171 var resolver;
172 var results = [];
173 var resultPromise = new Promise(function(r) { resolver = r; });
174
175 if (promises.length === 0) {
176 resolver.resolve(undefined);
177 return resultPromise;
178 }
179 promises.forEach(function(promise) {
180 if (!(promise instanceof Promise)) {
181 throw TypeError();
182 }
183 // We use Promise.prototype.then instead of |append| function.
184 promise.then(function(result) {
185 resolver.resolve(result, true);
186 }, function(result) {
187 resolver.reject(result, true);
188 });
189 });
190 return resultPromise;
191 }
192
193 /**
194 * Promise.some API.
195 * @parameters {...Promise} promises
196 */
197 function some()
198 {
199 var promises = Array.prototype.slice.call(arguments, 0);
200 var resolver;
201 var countdown = promises.length;
202 var results = [];
203 var resultPromise = new Promise(function(r) { resolver = r; });
204
205 if (promises.length === 0) {
206 resolver.resolve(undefined);
207 return resultPromise;
208 }
209 promises.forEach(function(promise, index) {
210 if (!(promise instanceof Promise)) {
211 throw TypeError();
212 }
213 // We use Promise.prototype.then instead of |append| function.
214 promise.then(function (result) {
215 resolver.resolve(result, true);
216 }, function(result) {
217 results[index] = result;
218 if (--countdown === 0) {
219 resolver.reject(results, true);
220 }
221 });
222 });
223 return resultPromise;
224 }
225
226 /**
227 * Promise constructor.
228 * Note that this differs from Promise API.
229 * Blink bridge fills the gap.
230 */
231 function $Promise(callback)
232 {
233 this.associated = {
234 state: 'pending',
235 result: undefined,
236 fulfillCallbacks: [],
237 rejectCallbacks: []
238 };
239 }
240
241 $Promise.prototype = {
242 append: append,
243 then: then,
244 'catch': catch_
245 };
246 $Promise.fulfill = fulfill;
247 $Promise.resolve = resolve;
248 $Promise.reject = reject;
249 $Promise.every = every;
250 $Promise.any = any;
251 $Promise.some = some;
252 return Object.freeze($Promise);
abarth-chromium 2013/06/14 05:07:08 Why Object.freeze?
253 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698