Skip to content

Adds Promise.withResolvers #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions polyfills/Promise/withResolvers/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
aliases = [ "es2024" ]
dependencies = [
"_ESAbstract.CreateDataPropertyOrThrow",
"_ESAbstract.CreateMethodProperty",
"_ESAbstract.IsConstructor",
"Promise",
]
license = "MIT"
spec = "https://tc39.es/ecma262/#sec-promise.withResolvers"
docs = "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers"

[browsers]
android = "*"
bb = "*"
chrome = "<119"
edge = "*"
edge_mob = "*"
firefox = "<121"
firefox_mob = "<121"
ie = "*"
ie_mob = "*"
opera = "<105"
op_mob = "<79"
op_mini = "*"
safari = "<17.4"
ios_saf = "<17.4"
samsung_mob = "*"
1 change: 1 addition & 0 deletions polyfills/Promise/withResolvers/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Promise" in self && "withResolvers" in self.Promise;
27 changes: 27 additions & 0 deletions polyfills/Promise/withResolvers/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* global CreateDataPropertyOrThrow, CreateMethodProperty, IsConstructor, Promise */
(function () {
// 27.2.4.8 Promise.withResolvers ( )
CreateMethodProperty(Promise, "withResolvers", function withResolvers() {
// 1. Let C be the this value.
var C = this;
// 2. Let promiseCapability be ? NewPromiseCapability(C).
if (IsConstructor(C) === false) {
throw new TypeError("The `this` value must be a constructor");
}
var promiseCapability = {};
promiseCapability["[[Promise]]"] = new C(function (resolve, reject) {
promiseCapability["[[Resolve]]"] = resolve;
promiseCapability["[[Reject]]"] = reject;
});
// 3. Let obj be OrdinaryObjectCreate(%Object.prototype%).
var obj = {};
// 4. Perform ! CreateDataPropertyOrThrow(obj, "promise", promiseCapability.[[Promise]]).
CreateDataPropertyOrThrow(obj, "promise", promiseCapability["[[Promise]]"]);
// 5. Perform ! CreateDataPropertyOrThrow(obj, "resolve", promiseCapability.[[Resolve]]).
CreateDataPropertyOrThrow(obj, "resolve", promiseCapability["[[Resolve]]"]);
// 6. Perform ! CreateDataPropertyOrThrow(obj, "reject", promiseCapability.[[Reject]]).
CreateDataPropertyOrThrow(obj, "reject", promiseCapability["[[Reject]]"]);
// 7. Return obj.
return obj;
});
})();
66 changes: 66 additions & 0 deletions polyfills/Promise/withResolvers/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* globals proclaim, Promise */

it("is a function", function () {
proclaim.isFunction(Promise.withResolvers);
});

it("has correct arity", function () {
proclaim.arity(Promise.withResolvers, 0);
});

it("is not enumerable", function () {
proclaim.isNotEnumerable(Promise, "withResolvers");
});

describe("withResolvers", function () {
it("returns an object with `promise`, `resolve`, and `reject`", function () {
var promiseWithResolvers = Promise.withResolvers();
var promise = promiseWithResolvers.promise;
var resolve = promiseWithResolvers.resolve;
var reject = promiseWithResolvers.reject;
proclaim.ok(promise instanceof Promise);
proclaim.ok(resolve instanceof Function);
proclaim.ok(reject instanceof Function);
});

it("resolves the Promise when `resolve` is called", function () {
var promiseWithResolvers = Promise.withResolvers();
var promise = promiseWithResolvers.promise;
var resolve = promiseWithResolvers.resolve;

resolve("ok");

return promise.then(function (value) {
proclaim.equal(value, "ok");
});
});

it("rejects the Promise when `reject` is called", function () {
var promiseWithResolvers = Promise.withResolvers();
var promise = promiseWithResolvers.promise;
var reject = promiseWithResolvers.reject;

reject("not ok");

return promise.catch(function (error) {
proclaim.equal(error, "not ok");
});
});

it("throws a TypeError for non-constructor `this`", function () {
proclaim.throws(function () {
Promise.withResolvers.call({});
}, TypeError);
});

it("supports `this` as a Promise subclass", function () {
function _Promise(executor) {
return new Promise(executor);
}
_Promise.prototype = Promise.prototype;

var _promise = Promise.withResolvers.call(_Promise).promise;

proclaim.ok(_promise instanceof _Promise);
});
});