-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Promise.prototype.finally shim
- Loading branch information
Showing
11 changed files
with
143 additions
and
1 deletion.
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
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,10 @@ | ||
"use strict"; | ||
|
||
if (!require("./is-implemented")()) { | ||
Object.defineProperty(Promise.prototype, "finally", { | ||
value: require("./shim"), | ||
configurable: true, | ||
enumerable: false, | ||
writable: 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,3 @@ | ||
"use strict"; | ||
|
||
module.exports = require("./is-implemented")() ? Promise.prototype.finally : require("./shim"); |
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,7 @@ | ||
"use strict"; | ||
|
||
module.exports = function () { | ||
if (typeof Promise !== "function") return false; | ||
if (typeof Promise.prototype.finally !== "function") return false; | ||
return 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,24 @@ | ||
"use strict"; | ||
|
||
var ensurePlainFunction = require("../../../object/ensure-plain-function") | ||
, isThenable = require("../../../object/is-thenable") | ||
, ensureThenable = require("../../../object/ensure-thenable"); | ||
|
||
var resolveCallback = function (callback, next) { | ||
var callbackResult = callback(); | ||
if (!isThenable(callbackResult)) return next(); | ||
return callbackResult.then(next); | ||
}; | ||
|
||
module.exports = function (callback) { | ||
ensureThenable(this); | ||
ensurePlainFunction(callback); | ||
return this.then( | ||
function (result) { | ||
return resolveCallback(callback, function () { return result; }); | ||
}, | ||
function (error) { | ||
return resolveCallback(callback, function () { throw error; }); | ||
} | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"use strict"; | ||
|
||
module.exports = { asCallback: require("./as-callback") }; | ||
module.exports = { asCallback: require("./as-callback"), finally: require("./finally") }; |
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,7 @@ | ||
"use strict"; | ||
|
||
var isImplemented = require("../../../../promise/#/finally/is-implemented"); | ||
|
||
if (typeof Promise !== "function") global.Promise = require("plain-promise"); | ||
|
||
module.exports = function (a) { a(isImplemented(), 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,3 @@ | ||
"use strict"; | ||
|
||
module.exports = require("./shim"); |
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 = function (t, a) { | ||
a(typeof t(), "boolean"); | ||
}; |
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,75 @@ | ||
"use strict"; | ||
|
||
var microtaskDelay = require("../../../../function/#/microtask-delay"); | ||
|
||
if (typeof Promise !== "function") global.Promise = require("plain-promise"); | ||
|
||
module.exports = function (t, a) { | ||
return { | ||
Success: function (d) { | ||
var invoked; | ||
t.call(Promise.resolve("foo"), function () { | ||
invoked = true; | ||
return "bar"; | ||
}).then( | ||
microtaskDelay.call(function (result) { | ||
a(result, "foo"); | ||
a(invoked, true); | ||
d(); | ||
}, microtaskDelay.call(d)) | ||
); | ||
}, | ||
Failure: function (d) { | ||
var invoked; | ||
var error = new Error("Some error"); | ||
t.call(Promise.reject(error), function () { | ||
invoked = true; | ||
return "bar"; | ||
}).then( | ||
microtaskDelay.call(function () { | ||
a.never(); | ||
d(); | ||
}), | ||
microtaskDelay.call(function (result) { | ||
a(result, error); | ||
a(invoked, true); | ||
d(); | ||
}) | ||
); | ||
}, | ||
SuccessFinallyError: function (d) { | ||
var invoked, finallyError = new Error("Finally error"); | ||
t.call(Promise.resolve("foo"), function () { | ||
invoked = true; | ||
throw finallyError; | ||
}).then( | ||
microtaskDelay.call(function () { | ||
a.never(); | ||
d(); | ||
}), | ||
microtaskDelay.call(function (result) { | ||
a(result, finallyError); | ||
a(invoked, true); | ||
d(); | ||
}) | ||
); | ||
}, | ||
FailureFinallyError: function (d) { | ||
var invoked, finallyError = new Error("Finally error"); | ||
t.call(Promise.reject(new Error("Some error")), function () { | ||
invoked = true; | ||
throw finallyError; | ||
}).then( | ||
microtaskDelay.call(function () { | ||
a.never(); | ||
d(); | ||
}), | ||
microtaskDelay.call(function (result) { | ||
a(result, finallyError); | ||
a(invoked, true); | ||
d(); | ||
}) | ||
); | ||
} | ||
}; | ||
}; |