lazy.js
858 Bytes
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
"use strict";
var isFunction = require("../function/is-function");
module.exports = function (executor) {
var Constructor;
if (isFunction(this)) {
Constructor = this;
} else if (typeof Promise === "function") {
Constructor = Promise;
} else {
throw new TypeError("Could not resolve Promise constuctor");
}
var lazyThen;
var promise = new Constructor(function (resolve, reject) {
lazyThen = function (onSuccess, onFailure) {
if (!hasOwnProperty.call(this, "then")) {
// Sanity check
throw new Error("Unexpected (inherited) lazy then invocation");
}
try {
executor(resolve, reject);
} catch (reason) {
reject(reason);
}
delete this.then;
return this.then(onSuccess, onFailure);
};
});
return Object.defineProperty(promise, "then", {
configurable: true,
writable: true,
value: lazyThen
});
};