Skip to content

Commit

Permalink
feat: Promise.lazy
Browse files Browse the repository at this point in the history
Constructor for lazy promises
  • Loading branch information
medikoo committed Feb 16, 2018
1 parent 78ed73b commit 7a30a78
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
};
3 changes: 3 additions & 0 deletions promise/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"globals": { "Promise": true }
}
5 changes: 5 additions & 0 deletions promise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

module.exports = {
lazy: require("./lazy")
};
38 changes: 38 additions & 0 deletions promise/lazy.js
Original file line number Diff line number Diff line change
@@ -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
});
};
3 changes: 3 additions & 0 deletions test/promise/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"globals": { "setTimeout": true }
}
52 changes: 52 additions & 0 deletions test/promise/lazy.js
Original file line number Diff line number Diff line change
@@ -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
});
}
};
};

0 comments on commit 7a30a78

Please sign in to comment.