Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * THIS MODULE IS DEPRECATED. IMPORT "Promise.jsm" INSTEAD. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | "use strict"; |
michael@0 | 10 | |
michael@0 | 11 | this.Promise = {}; |
michael@0 | 12 | |
michael@0 | 13 | if (typeof(require) === "function") { |
michael@0 | 14 | module.exports = Promise; |
michael@0 | 15 | } else { |
michael@0 | 16 | this.EXPORTED_SYMBOLS = ["Promise"]; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | function fulfilled(value) { |
michael@0 | 20 | return { then: function then(fulfill) { fulfill(value); } }; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | function rejected(reason) { |
michael@0 | 24 | return { then: function then(fulfill, reject) { reject(reason); } }; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | function isPromise(value) { |
michael@0 | 28 | return value && typeof(value.then) === 'function'; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | function defer() { |
michael@0 | 32 | var observers = []; |
michael@0 | 33 | var result = null; |
michael@0 | 34 | var promise = { |
michael@0 | 35 | then: function then(onFulfill, onError) { |
michael@0 | 36 | var deferred = defer(); |
michael@0 | 37 | |
michael@0 | 38 | function resolve(value) { |
michael@0 | 39 | try { |
michael@0 | 40 | deferred.resolve(onFulfill ? onFulfill(value) : value); |
michael@0 | 41 | } catch (error) { |
michael@0 | 42 | deferred.resolve(rejected(error)); |
michael@0 | 43 | } |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | function reject(reason) { |
michael@0 | 47 | try { |
michael@0 | 48 | if (onError) deferred.resolve(onError(reason)); |
michael@0 | 49 | else deferred.resolve(rejected(reason)); |
michael@0 | 50 | } catch (error) { |
michael@0 | 51 | deferred.resolve(rejected(error)); |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | if (observers) { |
michael@0 | 56 | observers.push({ resolve: resolve, reject: reject }); |
michael@0 | 57 | } else { |
michael@0 | 58 | result.then(resolve, reject); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | return deferred.promise; |
michael@0 | 62 | } |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | var deferred = { |
michael@0 | 66 | promise: promise, |
michael@0 | 67 | resolve: function resolve(value) { |
michael@0 | 68 | if (!result) { |
michael@0 | 69 | result = isPromise(value) ? value : fulfilled(value); |
michael@0 | 70 | while (observers.length) { |
michael@0 | 71 | var observer = observers.shift(); |
michael@0 | 72 | result.then(observer.resolve, observer.reject); |
michael@0 | 73 | } |
michael@0 | 74 | observers = null; |
michael@0 | 75 | } |
michael@0 | 76 | }, |
michael@0 | 77 | reject: function reject(reason) { |
michael@0 | 78 | deferred.resolve(rejected(reason)); |
michael@0 | 79 | } |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | return deferred; |
michael@0 | 83 | } |
michael@0 | 84 | Promise.defer = defer; |
michael@0 | 85 | |
michael@0 | 86 | function resolve(value) { |
michael@0 | 87 | var deferred = defer(); |
michael@0 | 88 | deferred.resolve(value); |
michael@0 | 89 | return deferred.promise; |
michael@0 | 90 | } |
michael@0 | 91 | Promise.resolve = resolve; |
michael@0 | 92 | |
michael@0 | 93 | function reject(reason) { |
michael@0 | 94 | var deferred = defer(); |
michael@0 | 95 | deferred.reject(reason); |
michael@0 | 96 | return deferred.promise; |
michael@0 | 97 | } |
michael@0 | 98 | Promise.reject = reject; |
michael@0 | 99 | |
michael@0 | 100 | var promised = (function() { |
michael@0 | 101 | var call = Function.call; |
michael@0 | 102 | var concat = Array.prototype.concat; |
michael@0 | 103 | function execute(args) { return call.apply(call, args) } |
michael@0 | 104 | function promisedConcat(promises, unknown) { |
michael@0 | 105 | return promises.then(function(values) { |
michael@0 | 106 | return resolve(unknown).then(function(value) { |
michael@0 | 107 | return values.concat([ value ]); |
michael@0 | 108 | }); |
michael@0 | 109 | }); |
michael@0 | 110 | } |
michael@0 | 111 | return function promised(f) { |
michael@0 | 112 | return function promised() { |
michael@0 | 113 | return concat.apply([ f, this ], arguments). |
michael@0 | 114 | reduce(promisedConcat, resolve([])). |
michael@0 | 115 | then(execute); |
michael@0 | 116 | }; |
michael@0 | 117 | } |
michael@0 | 118 | })(); |
michael@0 | 119 | Promise.all = promised(Array); |