Skip to content

Commit

Permalink
[Bug] doAwaitResponse should return / handle any additional value ret…
Browse files Browse the repository at this point in the history
…urned by the callback function #108
  • Loading branch information
nev21 committed Dec 13, 2023
1 parent 1eb5435 commit c7918d7
Show file tree
Hide file tree
Showing 10 changed files with 1,850 additions and 1,042 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ jobs:

strategy:
matrix:
node: [ 16, 18, 20 ]
node: [ 14, 16, 18 ]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm install
- name: Update rush shrinkwrap dependencies
run: node common/scripts/install-run-rush.js update --full
- name: Npm Install
run: npm install
- name: Build
run: npm run build --verbose
timeout-minutes: 10
Expand Down
2,506 changes: 1,509 additions & 997 deletions common/config/rush/npm-shrinkwrap.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion common/scripts/install-run-rush.js

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

2 changes: 1 addition & 1 deletion common/scripts/install-run-rushx.js

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

70 changes: 54 additions & 16 deletions common/scripts/install-run.js

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

2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"karma-coverage": "^2.2.0",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"puppeteer": "^19.0.0",
"puppeteer": "^21.6.0",
"rollup": "^3.8.1",
"rollup-plugin-minify-es": "^1.1.1",
"rollup-plugin-istanbul": "^4.0.0",
Expand Down
133 changes: 118 additions & 15 deletions lib/src/promise/await.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,123 @@ import { FinallyPromiseHandler, RejectedPromiseHandler, ResolvedPromiseHandler }
* Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param cb - The callback to call with the response of the promise as an IAwaitResponse object.
* @param cb - The callback to call with the value, if the value is a promise then the callback will be
* called with the result of the promise as an IAwaitResponse object, it will be called whether any promise
* resolves or rejects.
* @returns The value returned by the `cb` callback function, if the value is a promise then the return value
* of the callback will be returned as a promise.
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via doAwaitResponse
* doAwaitResponse(promise, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // Do something with the reason
* }
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwaitResponse(42, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // This will never be true as the value is not a promise
* }
* });
* ```
*/
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | Promise<T>, cb: (response: AwaitResponse<T | TResult1>) => T | TResult1 | TResult2 | Promise<T | TResult1 | TResult2>): T | TResult1 | TResult2 | Promise<T | TResult1 | TResult2>;

/**
* Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param cb - The callback to call with the value, if the value is a promise then the callback will be
* called with the result of the promise as an IAwaitResponse object, it will be called whether any promise
* resolves or rejects.
* @returns The value returned by the `cb` callback function, if the value is a promise then the return value
* of the callback will be returned as a promise.
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via doAwaitResponse
* doAwaitResponse(promise, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // Do something with the reason
* }
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwaitResponse(42, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // This will never be true as the value is not a promise
* }
* });
* ```
*/
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | Promise<T>, cb: (response: AwaitResponse<T>) => void): T | Promise<T | TResult1 | TResult2>;
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | PromiseLike<T>, cb: (response: AwaitResponse<T>) => void): T | PromiseLike<T | TResult1 | TResult2>;
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | IPromise<T>, cb: (response: AwaitResponse<T>) => void): T | IPromise<T | TResult1 | TResult2> {
return doAwait(value as any, (value) => {
cb && cb({
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | PromiseLike<T>, cb: (response: AwaitResponse<T | TResult1>) => T | TResult1 | TResult2 | PromiseLike<T | TResult1 | TResult2>): T | TResult1 | TResult2 | PromiseLike<T | TResult1 | TResult2>;

/**
* Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param cb - The callback to call with the value, if the value is a promise then the callback will be
* called with the result of the promise as an IAwaitResponse object, it will be called whether any promise
* resolves or rejects.
* @returns The value returned by the `cb` callback function, if the value is a promise then the return value
* of the callback will be returned as a promise.
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via doAwaitResponse
* doAwaitResponse(promise, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // Do something with the reason
* }
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwaitResponse(42, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // This will never be true as the value is not a promise
* }
* });
* ```
*/
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | IPromise<T>, cb: (response: AwaitResponse<T | TResult1>) => T | TResult1 | TResult2 | IPromise<T | TResult1 | TResult2>): T | TResult1 | TResult2 | IPromise<T | TResult1 | TResult2> {
return doAwait(value as T, (value) => {
return cb ? cb({
value: value,
rejected: false
});
}) : value;
},
(reason) => {
cb && cb({
return cb ? cb({
rejected: true,
reason: reason
});
}) : reason;
});
}

Expand Down Expand Up @@ -69,7 +170,7 @@ export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | IP
* });
* ```
*/
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | Promise<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): T | Promise<T | TResult1 | TResult2>;
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | Promise<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): T | TResult1 | Promise<T | TResult1 | TResult2>;

/**
* Wait for the promise to resolve or reject, if resolved the callback function will be called with it's value and if
Expand Down Expand Up @@ -106,7 +207,7 @@ export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | Promise<T>
* });
* ```
*/
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | PromiseLike<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): T | PromiseLike<T | TResult1 | TResult2>;
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | PromiseLike<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): T | TResult1 | PromiseLike<T | TResult1 | TResult2>;

/**
* Wait for the promise to resolve or reject, if resolved the callback function will be called with it's value and if
Expand Down Expand Up @@ -142,19 +243,21 @@ export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | PromiseLik
* });
* ```
*/
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | IPromise<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): T | IPromise<T | TResult1 | TResult2> {
let result = value;
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | IPromise<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): T | TResult1 | IPromise<T | TResult1 | TResult2> {
let result: T | TResult1 | IPromise<T | TResult1> | PromiseLike<TResult1> = value;

if (isPromiseLike<T>(value)) {
if (resolveFn || rejectFn) {
result = value.then(resolveFn, rejectFn) as any;
}
} else {
resolveFn && resolveFn(value as T);
if (resolveFn) {
result = resolveFn(value);
}
}

if (finallyFn) {
result = doFinally(result as any, finallyFn);
doFinally(result as any, finallyFn);
}

return result as any;
Expand Down
Loading

0 comments on commit c7918d7

Please sign in to comment.