mirror of
https://github.com/hanzoai/es5-ext.git
synced 2026-09-24 16:58:43 +00:00
feat: Promise.lazy
Constructor for lazy promises
This commit is contained in:
@@ -16,6 +16,7 @@ module.exports = {
|
||||
math: require("./math"),
|
||||
number: require("./number"),
|
||||
object: require("./object"),
|
||||
promise: require("./promise"),
|
||||
regExp: require("./reg-exp"),
|
||||
string: require("./string")
|
||||
};
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"globals": { "Promise": true }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
lazy: require("./lazy")
|
||||
};
|
||||
@@ -0,0 +1,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
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"globals": { "setTimeout": true }
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function (t) {
|
||||
if (typeof Promise !== "function") return null; // Run tests only in ES2015+ env
|
||||
|
||||
return {
|
||||
"Delays execution": function (a, d) {
|
||||
var invoked = false;
|
||||
var promise = t(function (resolve) {
|
||||
invoked = true;
|
||||
setTimeout(function () {
|
||||
resolve(20);
|
||||
}, 10);
|
||||
});
|
||||
|
||||
a(invoked, false);
|
||||
|
||||
setTimeout(function () {
|
||||
a(invoked, false);
|
||||
promise.then(function (value) {
|
||||
a(value, 20);
|
||||
setTimeout(d, 0); // Escape error swallowing
|
||||
});
|
||||
a(invoked, true);
|
||||
}, 15);
|
||||
},
|
||||
"Passes rejection": function (a, d) {
|
||||
var promise = t(function (resolve, reject) {
|
||||
setTimeout(function () {
|
||||
reject(new Error("Stop"));
|
||||
}, 10);
|
||||
});
|
||||
|
||||
promise.catch(function (error) {
|
||||
a(error instanceof Error, true);
|
||||
a(error.message, "Stop");
|
||||
setTimeout(d, 0); // Escape error swallowing
|
||||
});
|
||||
},
|
||||
"Passes sync exception": function (a, d) {
|
||||
var promise = t(function () {
|
||||
throw new Error("Stop");
|
||||
});
|
||||
|
||||
promise.catch(function (error) {
|
||||
a(error instanceof Error, true);
|
||||
a(error.message, "Stop");
|
||||
setTimeout(d, 0); // Escape error swallowing
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user