-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Constructor for lazy promises
- Loading branch information
Showing
6 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"globals": { "Promise": true } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
lazy: require("./lazy") | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"globals": { "setTimeout": true } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
}; | ||
}; |