Skip to content

Commit

Permalink
update polyfill Promise to v8.3.0
Browse files Browse the repository at this point in the history
Summary:
Update Promise to support `Promise.any` out of box

- updated npm package
- updated code in `01-Promise.js` in align with changes in from https://github.com/then/promise/blob/8.2.0/src/es6-extensions.js
- added test case

Reviewed By: motiz88

Differential Revision: D40711825

fbshipit-source-id: 4981fce6722be3a1cd81d0f660e3f493ef38959f
  • Loading branch information
jacdebug authored and facebook-github-bot committed Oct 27, 2022
1 parent 1900bad commit e97db61
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
44 changes: 44 additions & 0 deletions lib/InternalBytecode/01-Promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,50 @@
return this.then(null, onRejected);
};

function getAggregateError(errors){
if(typeof AggregateError === 'function'){
return new AggregateError(errors,'All promises were rejected');
}

var error = new Error('All promises were rejected');

error.name = 'AggregateError';
error.errors = errors;

return error;
}

core.any = function promiseAny(values) {
return new core(function(resolve, reject) {
var promises = iterableToArray(values);
var hasResolved = false;
var rejectionReasons = [];

function resolveOnce(value) {
if (!hasResolved) {
hasResolved = true;
resolve(value);
}
}

function rejectionCheck(reason) {
rejectionReasons.push(reason);

if (rejectionReasons.length === promises.length) {
reject(getAggregateError(rejectionReasons));
}
}

if(promises.length === 0){
reject(getAggregateError(rejectionReasons));
} else {
promises.forEach(function(value){
core.resolve(value).then(resolveOnce, rejectionCheck);
});
}
});
};

core.prototype.finally = function (f) {
return this.then(function (value) {
return core.resolve(f()).then(function () {
Expand Down
3 changes: 3 additions & 0 deletions test/hermes/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ print('all' in Promise);
print('allSettled' in Promise);
// CHECK-NEXT: true

print('any' in Promise);
// CHECK-NEXT: true

var promise = new Promise(function(res, rej) {
res('success!');
});
Expand Down
2 changes: 1 addition & 1 deletion utils/promise/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dependencies": {
"@rollup/plugin-commonjs": "~15.0.0",
"@rollup/plugin-node-resolve": "~9.0.0",
"promise": "8.2.0"
"promise": "8.3.0"
},
"scripts": {
"start": "yarn run rollup --config rollup.config.js index.js"
Expand Down
8 changes: 4 additions & 4 deletions utils/promise/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ picomatch@^2.2.2:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==

promise@8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/promise/-/promise-8.2.0.tgz#a1f6280ab67457fbfc8aad2b198c9497e9e5c806"
integrity sha512-+CMAlLHqwRYwBMXKCP+o8ns7DN+xHDUiI+0nArsiJ9y+kJVPLFxEaSw6Ha9s9H0tftxg2Yzl25wqj9G7m5wLZg==
promise@8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/promise/-/promise-8.3.0.tgz#8cb333d1edeb61ef23869fbb8a4ea0279ab60e0a"
integrity sha512-rZPNPKTOYVNEEKFaq1HqTgOwZD+4/YHS5ukLzQCypkj+OkYx7iv0mA91lJlpPPZ8vMau3IIGj5Qlwrx+8iiSmg==
dependencies:
asap "~2.0.6"

Expand Down

0 comments on commit e97db61

Please sign in to comment.