Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / third_party / spaceport / js / util / quickPromise.js
blob0872247dba183e6fab6e00e1bb746994e1466e24
1 define([ 'util/ensureCallback' ], function (ensureCallback) {
2     function quickPromise() {
3         var thens = [ ];
4         var resolved = false;
6         function resolve() {
7             if (resolved) {
8                 throw new Error("Already resolved");
9             }
10             resolved = true;
12             while (thens.length) {
13                 var fn = thens.pop();
14                 fn();
15             }
16         }
18         function then(fn) {
19             fn = ensureCallback(fn);
20             if (resolved) {
21                 fn();
22             } else {
23                 thens.push(fn);
24             }
25         }
27         return {
28             resolve: resolve,
29             then: then
30         };
31     }
33     return quickPromise;
34 });