summaryrefslogtreecommitdiffstats
path: root/devtools/shared/deprecated-sync-thenables.js
blob: 52bf671cd8c4ac51bac93fad5f97601d7817b055 (plain)
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
115
116
117
118
119
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * THIS MODULE IS DEPRECATED. IMPORT "Promise.jsm" INSTEAD.
 */

"use strict";

this.Promise = {};

if (typeof (require) === "function") {
  module.exports = Promise;
} else {
  this.EXPORTED_SYMBOLS = ["Promise"];
}

function fulfilled(value) {
  return { then: function then(fulfill) { fulfill(value); } };
}

function rejected(reason) {
  return { then: function then(fulfill, reject) { reject(reason); } };
}

function isPromise(value) {
  return value && typeof (value.then) === "function";
}

function defer() {
  var observers = [];
  var result = null;
  var promise = {
    then: function then(onFulfill, onError) {
      var deferred = defer();

      function resolve(value) {
        try {
          deferred.resolve(onFulfill ? onFulfill(value) : value);
        } catch (error) {
          deferred.resolve(rejected(error));
        }
      }

      function reject(reason) {
        try {
          if (onError) deferred.resolve(onError(reason));
          else deferred.resolve(rejected(reason));
        } catch (error) {
          deferred.resolve(rejected(error));
        }
      }

      if (observers) {
        observers.push({ resolve: resolve, reject: reject });
      } else {
        result.then(resolve, reject);
      }

      return deferred.promise;
    }
  };

  var deferred = {
    promise: promise,
    resolve: function resolve(value) {
      if (!result) {
        result = isPromise(value) ? value : fulfilled(value);
        while (observers.length) {
          var observer = observers.shift();
          result.then(observer.resolve, observer.reject);
        }
        observers = null;
      }
    },
    reject: function reject(reason) {
      deferred.resolve(rejected(reason));
    }
  };

  return deferred;
}
Promise.defer = defer;

function resolve(value) {
  var deferred = defer();
  deferred.resolve(value);
  return deferred.promise;
}
Promise.resolve = resolve;

function reject(reason) {
  var deferred = defer();
  deferred.reject(reason);
  return deferred.promise;
}
Promise.reject = reject;

var promised = (function () {
  var call = Function.call;
  var concat = Array.prototype.concat;
  function execute(args) { return call.apply(call, args); }
  function promisedConcat(promises, unknown) {
    return promises.then(function (values) {
      return resolve(unknown).then(function (value) {
        return values.concat([ value ]);
      });
    });
  }
  return function promised(f) {
    return function promised() {
      return concat.apply([ f, this ], arguments).
        reduce(promisedConcat, resolve([])).
        then(execute);
    };
  };
})();
Promise.all = promised(Array);