Skip to content

Commit

Permalink
fix async functions (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr authored Dec 14, 2022
1 parent 196a100 commit 7125543
Show file tree
Hide file tree
Showing 7 changed files with 808 additions and 503 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- '**'

env:
PRIMARY_NODE_VERSION: 16.x
PRIMARY_NODE_VERSION: 18.x
PRIMARY_OS: ubuntu-latest
REGISTRY: https://registry.npmjs.org/

Expand All @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
node-version: [ 6.x, 8.x, 10.x, 12.x, 14.x, 16.x, 18.x ]
node-version: [ 14.x, 16.x, 18.x ]

steps:

Expand Down Expand Up @@ -87,7 +87,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
node-version: [ 6.x ]
node-version: [ 18.x ]
needs: [ test ]
if: github.event_name == 'release' && github.event.action == 'published'
steps:
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## Development
- nothing yet

## [v4.0.0](https://github.com/regevbr/busywait.js/compare/v3.1.3...v4.0.0)
### BREAKING CHANGES
- Dropped support for node versions below 14
### Fixed
- functions with `async` modifier were not recognized as functions


## [v3.1.3](https://github.com/regevbr/busywait.js/compare/v3.1.2...v3.1.3)
### Fixed
- Support for node 18
Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "busywait",
"version": "3.1.3",
"version": "4.0.0",
"description": "Async busy wait",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -38,7 +38,7 @@
"backoff-strategy"
],
"engines": {
"node": "^6 || ^8 || ^10 || ^12 || ^14 || ^16 || ^18"
"node": "^14 || ^16 || ^18"
},
"files": [
"/dist/*.d.ts",
Expand All @@ -52,23 +52,23 @@
"homepage": "https://github.com/regevbr/busywait.js#readme",
"dependencies": {},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "0.1.3",
"@types/chai": "^4.2.17",
"@types/chai-as-promised": "^7.1.3",
"@types/mocha": "^8.2.2",
"@types/node": "^15.0.1",
"chai": "^4.3.4",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.15",
"chai": "^4.3.7",
"chai-as-promised": "^7.1.1",
"coveralls": "^3.1.0",
"coveralls": "^3.1.1",
"dirty-chai": "^2.0.1",
"mocha": "^6.2.3",
"nyc": "14.1.1",
"source-map-support": "^0.5.19",
"nyc": "^15.1.0",
"source-map-support": "^0.5.21",
"mocha-param": "^2.0.1",
"sinon": "^7.3.2",
"sinon-chai": "^3.6.0",
"ts-node": "^8.10.2",
"sinon": "^15.0.0",
"sinon-chai": "^3.7.0",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
"typescript": "^4.8.4"
"typescript": "^4.9.4"
}
}
40 changes: 4 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
/* istanbul ignore next */
// @ts-ignore
// tslint:disable-next-line:variable-name
const __awaiter = (thisArg, _arguments, P, generator) => {
function adopt(value: any) {
return value instanceof P ? value : new P((resolve: any) => {
resolve(value);
});
}

return new (P || (P = Promise))((resolve: any, reject: any) => {
function fulfilled(value: any) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}

function rejected(value: any) {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
}

function step(result: any) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}

step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};

type _CheckFn<T> = (iteration: number, delay: number, totalDelay: number) => T;
export type SyncCheckFn<T> = T extends Promise<any> ? never : _CheckFn<T>;
export type ASyncCheckFn<T> = _CheckFn<Promise<T>>;
Expand Down Expand Up @@ -62,7 +27,10 @@ type Resolve<T> = (result: T) => void;
type Reject = (error: Error) => void;
type Delayer = (iteration: number) => number;

const isFunction = (value: any): value is () => any => toString.call(value) === '[object Function]';
const isFunction = (value: any): value is () => any => {
const str = Object.prototype.toString.call(value);
return str === '[object Function]' || str === '[object AsyncFunction]';
}

const isNumber = (value: any): value is number => typeof value === 'number';

Expand Down
14 changes: 13 additions & 1 deletion src/test/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IParam {

type Done = (error?: any) => void;

describe('busywait.js', function() {
describe('busywait.js', function () {

this.timeout(10000);

Expand Down Expand Up @@ -85,9 +85,21 @@ describe('busywait.js', function() {
});
};

const nativeAsyncCheck = async (iteration: number, delay: number, totalDelay: number): Promise<string> => {
iterationsArray.push(iteration);
delaysArray.push(delay);
totalDelaysArray.push(totalDelay);
if (Date.now() > waitUntil) {
return successMessage;
} else {
throw new Error();
}
};

const params: IParam[] = [
{checkFn: syncCheck},
{checkFn: asyncCheck},
{checkFn: nativeAsyncCheck},
];

itParam('should complete', params, (param: IParam) => {
Expand Down
7 changes: 3 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2016",
"target": "ES2020",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
Expand All @@ -17,13 +17,12 @@
"strictFunctionTypes": true,
"alwaysStrict": true,
"noImplicitUseStrict": false,
"preserveSymlinks": true,
"preserveSymlinks": false,
"noEmitHelpers": true,
"rootDir": "src",
"sourceMap": true,
"lib": [
"es6",
"dom"
"ES2020"
]
},
"include": [
Expand Down
Loading

0 comments on commit 7125543

Please sign in to comment.