Skip to content

Commit

Permalink
feat: Promise.prototype.finally shim
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jan 16, 2019
1 parent 1df327f commit 4dadbc7
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ It's about properties introduced with ES6 and those that have been updated in ne
- `Object.assign` -> `require('es5-ext/object/assign')`
- `Object.keys` -> `require('es5-ext/object/keys')`
- `Object.setPrototypeOf` -> `require('es5-ext/object/set-prototype-of')`
- `Promise.prototype.finally` -> `require('es5-ext/promise/#/finally')`
- `RegExp.prototype.match` -> `require('es5-ext/reg-exp/#/match')`
- `RegExp.prototype.replace` -> `require('es5-ext/reg-exp/#/replace')`
- `RegExp.prototype.search` -> `require('es5-ext/reg-exp/#/search')`
Expand Down Expand Up @@ -842,6 +843,12 @@ Throws error if given value is not an object, otherwise it is returned.

Throws error if given value is `null` or `undefined`, otherwise returns value.

### Promise Prototype extensions

#### promise.finally(onFinally) _(es5-ext/promise/#/finally)_

[_Introduced with ECMAScript 2018_](https://tc39.github.io/ecma262/#sec-promise.prototype.finally).

### RegExp Constructor extensions

#### escape(str) _(es5-ext/reg-exp/escape)_
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"devDependencies": {
"eslint": "^5.11.1",
"eslint-config-medikoo-es5": "^1.7.3",
"plain-promise": "^0.1.1",
"tad": "~0.2.8"
},
"eslintConfig": {
Expand Down
10 changes: 10 additions & 0 deletions promise/#/finally/implement.js
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
});
}
3 changes: 3 additions & 0 deletions promise/#/finally/index.js
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");
7 changes: 7 additions & 0 deletions promise/#/finally/is-implemented.js
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;
};
24 changes: 24 additions & 0 deletions promise/#/finally/shim.js
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; });
}
);
};
2 changes: 1 addition & 1 deletion promise/#/index.js
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") };
7 changes: 7 additions & 0 deletions test/promise/#/finally/implement.js
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); };
3 changes: 3 additions & 0 deletions test/promise/#/finally/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("./shim");
5 changes: 5 additions & 0 deletions test/promise/#/finally/is-implemented.js
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");
};
75 changes: 75 additions & 0 deletions test/promise/#/finally/shim.js
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();
})
);
}
};
};

0 comments on commit 4dadbc7

Please sign in to comment.