Skip to content

Commit

Permalink
Merge pull request #46 from microsoft/geguskov/fix/promise-finally-po…
Browse files Browse the repository at this point in the history
…lyfill

Update Promise polyfills
  • Loading branch information
M1Les authored Jul 21, 2023
2 parents 88b0272 + 69ef513 commit 9347402
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/objectstoreprovider",
"version": "0.6.40",
"version": "0.6.41",
"description": "A cross-browser object store library",
"author": "Mukundan Kavanur Kidambi <[email protected]>",
"scripts": {
Expand Down Expand Up @@ -30,7 +30,7 @@
"sorted-btree": "^1.5.0"
},
"sideEffects": [
"./src/Promise.ts"
"./src/Promise.extensions.ts"
],
"devDependencies": {
"@types/assert": "^1.5.4",
Expand Down
2 changes: 1 addition & 1 deletion src/ObjectStoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import { noop, attempt, isError } from "lodash";
import "./Promise";
import "./Promise.extensions";
// Basic nomenclature types for everyone to agree on.
export type ItemType = object;
export type KeyComponentType = string | number | Date;
Expand Down
36 changes: 36 additions & 0 deletions src/Promise.extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// this is a module with side-effects that add polyfills for finally and always
// methods of the Promise

declare interface Promise<T> {
finally: (onfinally?: (() => void) | null | undefined) => Promise<T>;
always: <U>(func: (value: T | any) => U | PromiseLike<U>) => Promise<U>;
}

// polyfill for Promise.finally. It is part of ES6 spec now, but was not in it originally and has spotty support in older browsers:
// https://caniuse.com/mdn-javascript_builtins_promise_finally

if (typeof Promise.prototype.finally !== "function") {
Promise.prototype.finally = function (onResolveOrReject) {
const hasFunctionCallback = typeof onResolveOrReject === "function";
return this.catch(function (reason: any) {
hasFunctionCallback && onResolveOrReject && onResolveOrReject();
return Promise.reject(reason);
}).then(function (result: any) {
hasFunctionCallback && onResolveOrReject && onResolveOrReject();
return result;
});
};
}

// always polyfill, not part of the ES spec
if (typeof Promise.prototype.always !== "function") {
Promise.prototype.always = function (onResolveOrReject) {
return this.then(onResolveOrReject, function (reason: any) {
onResolveOrReject(reason);
throw reason;
});
};
}
18 changes: 0 additions & 18 deletions src/Promise.ts

This file was deleted.

0 comments on commit 9347402

Please sign in to comment.