OLD | NEW |
(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 fulfill(value, synchronous) |
| 33 { |
| 34 if (this.promise === undefined || this.promise.state !== 'pending') { |
| 35 return; |
| 36 } |
| 37 this.promise.state = 'fulfilled'; |
| 38 this.promise.result = value; |
| 39 var fulfillCallbacks = this.promise.fulfillCallbacks; |
| 40 this.promise.fulfillCallbacks = []; |
| 41 this.promise.rejectCallbacks = []; |
| 42 // Detach the associated information. We don't need it from now on. |
| 43 this.promise = undefined; |
| 44 |
| 45 if (synchronous) { |
| 46 for (var i = 0; i < fulfillCallbacks.length; ++i) { |
| 47 fulfillCallbacks[i](value); |
| 48 } |
| 49 } else { |
| 50 for (var i = 0; i < fulfillCallbacks.length; ++i) { |
| 51 postTask(fulfillCallbacks[i].bind(undefined, value)); |
| 52 } |
| 53 } |
| 54 } |
| 55 |
| 56 function resolve(value, synchronous) |
| 57 { |
| 58 if (this.promise === undefined || this.promise.state !== 'pending') { |
| 59 return; |
| 60 } |
| 61 var then = null; |
| 62 if (typeof value === 'object') { |
| 63 try { |
| 64 then = value.then; |
| 65 } catch (e) { |
| 66 this.reject(e, synchronous); |
| 67 return; |
| 68 } |
| 69 } |
| 70 if (typeof then === 'function') { |
| 71 try { |
| 72 var self = this; |
| 73 then.call(value, function(value) { |
| 74 self.resolve(value, true); |
| 75 }, function(value) { |
| 76 self.reject(value, true); |
| 77 }); |
| 78 } catch (e) { |
| 79 this.reject(e, synchronous); |
| 80 } |
| 81 return; |
| 82 } |
| 83 this.fulfill(value, synchronous); |
| 84 } |
| 85 |
| 86 function reject(value, synchronous) |
| 87 { |
| 88 if (this.promise === undefined || this.promise.state !== 'pending') { |
| 89 return; |
| 90 } |
| 91 this.promise.state = 'rejected'; |
| 92 this.promise.result = value; |
| 93 var rejectCallbacks = this.promise.rejectCallbacks; |
| 94 this.promise.fulfillCallbacks = []; |
| 95 this.promise.rejectCallbacks = []; |
| 96 // Detach the associated information. We don't need it from now on. |
| 97 this.promise = undefined; |
| 98 |
| 99 if (synchronous) { |
| 100 for (var i = 0; i < rejectCallbacks.length; ++i) { |
| 101 rejectCallbacks[i](value); |
| 102 } |
| 103 } else { |
| 104 for (var i = 0; i < rejectCallbacks.length; ++i) { |
| 105 postTask(rejectCallbacks[i].bind(undefined, value)); |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 /** |
| 111 * PromiseResolver constructor. |
| 112 * @param promiseAssociated promise associated information |
| 113 */ |
| 114 function $PromiseResolver(promiseAssociated) |
| 115 { |
| 116 this.promise = promiseAssociated; |
| 117 } |
| 118 |
| 119 $PromiseResolver.prototype = { |
| 120 fulfill: fulfill, |
| 121 resolve: resolve, |
| 122 reject: reject |
| 123 }; |
| 124 |
| 125 return Object.freeze($PromiseResolver); |
| 126 }); |
OLD | NEW |