Skip to content

Commit

Permalink
[Bug] SCRIPT5009: 'Promise' is undefined #50 (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSNev authored Apr 26, 2023
1 parent a041ec8 commit 4eb4484
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
52 changes: 26 additions & 26 deletions common/config/rush/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lib/src/internal/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* @nevware21/ts-async
* https://github.com/nevware21/ts-async
*
* Copyright (c) 2023 Nevware21
* Licensed under the MIT license.
*/

export const STR_PROMISE = "Promise";
5 changes: 3 additions & 2 deletions lib/src/promise/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from "../interfaces/types";
import { ePromiseState, STRING_STATES } from "../internal/state";
import { emitEvent } from "./event";
import { STR_PROMISE } from "../internal/constants";

const NODE_UNHANDLED_REJECTION = "unhandledRejection";
const UNHANDLED_REJECTION = NODE_UNHANDLED_REJECTION.toLowerCase();
Expand Down Expand Up @@ -62,7 +63,7 @@ export function _createPromise<T>(newPromise: PromiseCreatorFn, processor: Promi
let _unHandledRejectionHandler: ITimerHandler = null;
let _thePromise: IPromise<T>;

!_hasPromiseRejectionEvent && (_hasPromiseRejectionEvent = getLazy(() => !!getInst("PromiseRejectionEvent")));
!_hasPromiseRejectionEvent && (_hasPromiseRejectionEvent = getLazy(() => !!getInst(STR_PROMISE + "RejectionEvent")));

// https://tc39.es/ecma262/#sec-promise.prototype.then
const _then = <TResult1 = T, TResult2 = never>(onResolved?: ResolvedPromiseHandler<T, TResult1>, onRejected?: RejectedPromiseHandler<TResult2>): IPromise<TResult1 | TResult2> => {
Expand Down Expand Up @@ -247,7 +248,7 @@ export function _createPromise<T>(newPromise: PromiseCreatorFn, processor: Promi

(function _initialize() {
if (!isFunction(executor)) {
throwTypeError("Promise: executor is not a function - " + dumpFnObj(executor));
throwTypeError(STR_PROMISE + ": executor is not a function - " + dumpFnObj(executor));
}

const _rejectFn = _createSettleIfFn(ePromiseState.Rejected, ePromiseState.Pending);
Expand Down
20 changes: 14 additions & 6 deletions lib/src/promise/nativePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import { IPromise } from "../interfaces/IPromise";
import { ePromiseState, STRING_STATES } from "../internal/state";
import { PromiseExecutor } from "../interfaces/types";
import { dumpObj, getInst, getLazy, ILazyValue, isFunction, objDefineProp, throwTypeError } from "@nevware21/ts-utils";
import { STR_PROMISE } from "../internal/constants";

const PrmCls = Promise;
let _isPromiseSupported: ILazyValue<boolean>;
let _isPromiseSupported: ILazyValue<PromiseConstructor>;

/**
* Creates a Promise instance that when resolved or rejected will execute it's pending chained operations using the
* available native Promise implementation.
* If runtime does not support native `Promise` class (or no polyfill is available) this function will fallback to using
* `createAsyncPromise` which will resolve them __asynchronously__ using the optional provided timeout value to
* schedule when the chained items will be ececuted.
* schedule when the chained items will be executed.
* @group Alias
* @group Promise
* @group Native
Expand All @@ -30,13 +30,21 @@ let _isPromiseSupported: ILazyValue<boolean>;
* @param timeout - Optional timeout to wait before processing the items, defaults to zero.
*/
export function createNativePromise<T>(executor: PromiseExecutor<T>, timeout?: number): IPromise<T> {
!_isPromiseSupported && (_isPromiseSupported = getLazy(() => !!getInst("Promise")));
if (!_isPromiseSupported.v) {
!_isPromiseSupported && (_isPromiseSupported = getLazy(() => {
try {
return getInst(STR_PROMISE);
} catch(e) {
// eslint-disable-next-line no-empty
}
}));

const PrmCls = _isPromiseSupported.v;
if (!PrmCls) {
return createAsyncPromise(executor);
}

if (!isFunction(executor)) {
throwTypeError("Promise: executor is not a function - " + dumpObj(executor));
throwTypeError(STR_PROMISE + ": executor is not a function - " + dumpObj(executor));
}

let _state = ePromiseState.Pending;
Expand Down

0 comments on commit 4eb4484

Please sign in to comment.