├── .gitattributes
├── .stylelintrc.json
├── index.js
├── .gitignore
├── .npmignore
├── .editorconfig
├── inch.json
├── test
└── spec
│ ├── index-spec.js
│ └── node-later-spec.js
├── docs
├── CHANGELOG.md
└── api.md
├── .github
├── ISSUE_TEMPLATE
│ ├── feature_request.md
│ └── bug_report.md
├── workflows
│ └── ci.yml
└── CONTRIBUTING.md
├── .eslintrc.js
├── .jsbeautifyrc
├── package.json
├── lib
└── node-later.js
├── README.md
└── LICENSE
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text eol=lf
2 |
--------------------------------------------------------------------------------
/.stylelintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "stylelint-config-standard"
3 | }
4 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = require('./lib/node-later');
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | .temp
3 | node_modules
4 | bower_components
5 | npm-debug.log
6 | package-lock.json
7 | .nyc_output
8 | coverage
9 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | target
2 | .temp
3 | .nyc_output
4 | .config
5 | coverage
6 | bower_components
7 | .github
8 | test
9 | example
10 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 |
2 | root = true
3 |
4 | [*]
5 | charset = utf-8
6 | end_of_line = lf
7 | insert_final_newline = true
8 | trim_trailing_whitespace = true
9 | indent_style = space
10 | indent_size = 4
11 |
12 | [*.json]
13 | indent_size = 2
14 |
--------------------------------------------------------------------------------
/inch.json:
--------------------------------------------------------------------------------
1 | {
2 | "files": {
3 | "included": [
4 | "*.js",
5 | "lib/**/*.js",
6 | "tasks/**/*.js"
7 | ],
8 | "excluded": [
9 | "**/Gruntfile.js",
10 | "**/.eslintrc.js",
11 | "**/stylelint.config.js"
12 | ]
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/test/spec/index-spec.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const chai = require('chai');
4 | const assert = chai.assert;
5 | const later = require('../../');
6 |
7 | describe('Index', function () {
8 | it('node-later', function () {
9 | assert.isFunction(later);
10 | });
11 | });
12 |
--------------------------------------------------------------------------------
/docs/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | | Date | Version | Description |
2 | | ----------- | ------- | ----------- |
3 | | 2020-05-11 | v2.0.0 | Migrate to github actions and upgrade minimal node version |
4 | | 2019-12-16 | v1.0.16 | Maintenance |
5 | | 2017-04-28 | v1.0.0 | Offical release |
6 | | 2017-04-28 | v0.0.1 | Initial release |
7 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature Request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: sagiegurari
7 |
8 | ---
9 |
10 | ### Feature Description
11 |
12 |
13 | ### Describe The Solution You'd Like
14 |
15 |
16 | ### Code Sample
17 |
18 | ```js
19 | // paste code here
20 | ```
21 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug Report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: sagiegurari
7 |
8 | ---
9 |
10 | ### Describe The Bug
11 |
12 |
13 | ### To Reproduce
14 |
15 |
16 | ### Error Stack
17 |
18 | ```console
19 | The error stack trace
20 | ```
21 |
22 | ### Code Sample
23 |
24 | ```js
25 | // paste code here
26 | ```
27 |
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | 'env': {
3 | 'node': true,
4 | 'commonjs': true,
5 | 'es2021': true,
6 | 'mocha': true
7 | },
8 | 'extends': 'eslint:recommended',
9 | 'parserOptions': {
10 | 'ecmaVersion': 13
11 | },
12 | 'rules': {
13 | 'indent': [
14 | 'error',
15 | 4
16 | ],
17 | 'linebreak-style': [
18 | 'error',
19 | 'unix'
20 | ],
21 | 'quotes': [
22 | 'error',
23 | 'single'
24 | ],
25 | 'semi': [
26 | 'error',
27 | 'always'
28 | ]
29 | }
30 | };
31 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 | on: [push, pull_request]
3 | env:
4 | CLICOLOR_FORCE: 1
5 | jobs:
6 | ci:
7 | name: CI
8 | runs-on: ubuntu-latest
9 | strategy:
10 | fail-fast: false
11 | matrix:
12 | node-version: ['18.x']
13 | steps:
14 | - name: Checkout
15 | uses: actions/checkout@v2
16 | - name: Install node.js
17 | uses: actions/setup-node@v1
18 | with:
19 | node-version: ${{ matrix.node-version }}
20 | - name: Install Dependencies
21 | run: npm install
22 | - name: Run CI
23 | run: npm test
24 | - name: Coveralls
25 | uses: coverallsapp/github-action@master
26 | with:
27 | github-token: ${{ secrets.GITHUB_TOKEN }}
28 | path-to-lcov: './coverage/lcov.info'
29 |
--------------------------------------------------------------------------------
/.github/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contribution Guidelines
2 |
3 |
4 | ## Issues
5 |
6 | Found a bug? Got a question? Want some enhancement?
7 | First place to go is the repository issues section, and I'll try to help as much as possible.
8 |
9 | ## Pull Requests
10 |
11 | Fixed a bug or just want to provided additional functionality?
12 | Simply fork this repository, implement your changes and create a pull request.
13 | Few guidelines regarding pull requests:
14 |
15 | * This repository is integrated with github actions for continuous integration.
16 |
17 | Your pull request build must pass (the build will run automatically).
18 | You can run the following command locally to ensure the build will pass:
19 |
20 | ````sh
21 | npm test
22 | ````
23 |
24 | * This library is using multiple code inspection tools to validate certain level of standards.
The configuration is part of the repository and you can set your favorite IDE using that configuration.
You can run the following command locally to ensure the code inspection passes:
25 |
26 | ````sh
27 | npm run lint
28 | ````
29 |
30 | * There are many automatic unit tests as part of the library which provide full coverage of the functionality.
Any fix/enhancement must come with a set of tests to ensure it's working well.
31 |
--------------------------------------------------------------------------------
/.jsbeautifyrc:
--------------------------------------------------------------------------------
1 | {
2 | "js": {
3 | "indent_size": 4,
4 | "indent_char": " ",
5 | "eol": "\n",
6 | "indent_level": 0,
7 | "indent_with_tabs": false,
8 | "preserve_newlines": true,
9 | "max_preserve_newlines": 2,
10 | "space_in_paren": false,
11 | "jslint_happy": true,
12 | "space_after_anon_function": true,
13 | "brace_style": "collapse",
14 | "break_chained_methods": false,
15 | "keep_array_indentation": true,
16 | "unescape_strings": false,
17 | "wrap_line_length": 0,
18 | "end_with_newline": true,
19 | "comma_first": false,
20 | "eval_code": false,
21 | "keep_function_indentation": false,
22 | "space_before_conditional": true,
23 | "good_stuff": true
24 | },
25 | "css": {
26 | "indent_size": 2,
27 | "indent_char": " ",
28 | "indent_with_tabs": false,
29 | "eol": "\n",
30 | "end_with_newline": true,
31 | "selector_separator_newline": false,
32 | "newline_between_rules": true
33 | },
34 | "html": {
35 | "indent_size": 4,
36 | "indent_char": " ",
37 | "indent_with_tabs": false,
38 | "eol": "\n",
39 | "end_with_newline": true,
40 | "preserve_newlines": true,
41 | "max_preserve_newlines": 2,
42 | "indent_inner_html": true,
43 | "brace_style": "collapse",
44 | "indent_scripts": "normal",
45 | "wrap_line_length": 0,
46 | "wrap_attributes": "auto",
47 | "wrap_attributes_indent_size": 4
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "node-later",
3 | "version": "2.0.0",
4 | "description": "Defer function calls to the start of the next cycle.",
5 | "author": {
6 | "name": "Sagie Gur-Ari",
7 | "email": "sagiegurari@gmail.com"
8 | },
9 | "license": "Apache-2.0",
10 | "homepage": "http://github.com/sagiegurari/node-later",
11 | "repository": {
12 | "type": "git",
13 | "url": "http://github.com/sagiegurari/node-later.git"
14 | },
15 | "bugs": {
16 | "url": "http://github.com/sagiegurari/node-later/issues"
17 | },
18 | "keywords": [
19 | "async",
20 | "defer",
21 | "later",
22 | "timeout",
23 | "nextTick"
24 | ],
25 | "main": "index.js",
26 | "directories": {
27 | "lib": "lib",
28 | "test": "test/spec"
29 | },
30 | "scripts": {
31 | "clean": "rm -Rf ./.nyc_output ./coverage",
32 | "format": "js-beautify --config ./.jsbeautifyrc --file ./*.js ./lib/**/*.js ./test/**/*.js",
33 | "lint-js": "eslint ./*.js ./lib/**/*.js ./test/**/*.js",
34 | "lint-css": "stylelint --allow-empty-input ./docs/**/*.css",
35 | "lint": "npm run lint-js && npm run lint-css",
36 | "jstest": "mocha --exit ./test/spec/**/*.js",
37 | "coverage": "nyc --reporter=html --reporter=text --reporter=lcovonly --check-coverage=true mocha --exit ./test/spec/**/*.js",
38 | "docs": "jsdoc2md lib/**/*.js > ./docs/api.md",
39 | "test": "npm run clean && npm run format && npm run lint && npm run docs && npm run coverage",
40 | "postpublish": "git fetch && git pull"
41 | },
42 | "dependencies": {
43 | "semver": "^7"
44 | },
45 | "devDependencies": {
46 | "chai": "^4",
47 | "eslint": "^8",
48 | "js-beautify": "^1",
49 | "jsdoc-to-markdown": "^8",
50 | "mocha": "^10",
51 | "nyc": "^15",
52 | "stylelint": "^13",
53 | "stylelint-config-standard": "^22"
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/docs/api.md:
--------------------------------------------------------------------------------
1 | ## Functions
2 |
3 |
functionReturns a defer function.
6 |functionReturns a defer function.
9 |function
15 | Returns a defer function.
16 |
17 | **Kind**: global function
18 | **Returns**: function - A defer function
19 | **Access**: public
20 |
21 | | Param | Type | Default | Description |
22 | | --- | --- | --- | --- |
23 | | [ioSafe] | Boolean | false | True ensure IO safe implementation which will prevent stack overflow errors |
24 |
25 | **Example**
26 | ```js
27 | //get a defer function
28 | const defer = later();
29 |
30 | //use defer function
31 | defer(onCallback() {
32 | //do something
33 | });
34 | ```
35 |
36 |
37 | ## later([comptibleVersion]) ⇒ function
38 | Returns a defer function.
39 |
40 | **Kind**: global function
41 | **Returns**: function - A defer function
42 | **Access**: public
43 |
44 | | Param | Type | Default | Description |
45 | | --- | --- | --- | --- |
46 | | [comptibleVersion] | String | process.version.substring(1) | The nodejs version the defer function should be compatible with |
47 |
48 | **Example**
49 | ```js
50 | //get a defer function based on current nodejs version
51 | const defer = later();
52 |
53 | //use defer function
54 | defer(onCallback() {
55 | //do something
56 | });
57 |
58 | //get a defer function based on a specific node.js version
59 | defer = later('0.10.0'); //let be compatible with node.js 0.10 regardless of our current node.js runtime
60 |
61 | //use defer function
62 | defer(onCallback() {
63 | //do something
64 | });
65 | ```
66 |
--------------------------------------------------------------------------------
/lib/node-later.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const semver = require('semver');
4 |
5 | /*eslint-disable valid-jsdoc*/
6 | //jscs:disable jsDoc
7 | /**
8 | * Returns a defer function.
9 | *
10 | * @function
11 | * @alias later
12 | * @public
13 | * @param {Boolean} [ioSafe=false] - True ensure IO safe implementation which will prevent stack overflow errors
14 | * @returns {function} A defer function
15 | * @example
16 | * ```js
17 | * //get a defer function
18 | * const defer = later();
19 | *
20 | * //use defer function
21 | * defer(onCallback() {
22 | * //do something
23 | * });
24 | * ```
25 | *
26 | * @also
27 | *
28 | * Returns a defer function.
29 | *
30 | * @function
31 | * @alias later
32 | * @public
33 | * @param {String} [comptibleVersion=process.version.substring(1)] - The nodejs version the defer function should be compatible with
34 | * @returns {function} A defer function
35 | * @example
36 | * ```js
37 | * //get a defer function based on current nodejs version
38 | * const defer = later();
39 | *
40 | * //use defer function
41 | * defer(onCallback() {
42 | * //do something
43 | * });
44 | *
45 | * //get a defer function based on a specific node.js version
46 | * defer = later('0.10.0'); //let be compatible with node.js 0.10 regardless of our current node.js runtime
47 | *
48 | * //use defer function
49 | * defer(onCallback() {
50 | * //do something
51 | * });
52 | * ```
53 | */
54 | const later = function (comptibleVersionOrIOSafe) {
55 | let deferFn = process.nextTick;
56 |
57 | if (comptibleVersionOrIOSafe) {
58 | if (typeof comptibleVersionOrIOSafe === 'boolean') {
59 | deferFn = setImmediate;
60 | } else if (semver.lt(comptibleVersionOrIOSafe, '0.12.0')) {
61 | deferFn = setImmediate;
62 | }
63 | } else if (semver.lt(later.nodejsVersion, '0.12.0')) {
64 | deferFn = setImmediate;
65 | }
66 |
67 | return deferFn;
68 | };
69 | //jscs:enable jsDoc
70 | /*eslint-enable valid-jsdoc*/
71 |
72 | later.nodejsVersion = process.version.substring(1);
73 |
74 | module.exports = later;
75 |
--------------------------------------------------------------------------------
/test/spec/node-later-spec.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const chai = require('chai');
4 | const assert = chai.assert;
5 | const later = require('../../lib/node-later');
6 |
7 | describe('later', function () {
8 | describe('get', function () {
9 | it('undefined', function (done) {
10 | const defer = later();
11 | defer(done);
12 | });
13 |
14 | it('null', function (done) {
15 | const defer = later(null);
16 | defer(done);
17 | });
18 |
19 | it('ioSafe', function (done) {
20 | const defer = later(true);
21 | assert.strictEqual(setImmediate, defer);
22 | defer(done);
23 | });
24 |
25 | it('requested 0.12.0', function (done) {
26 | const defer = later('0.12.0');
27 | assert.strictEqual(process.nextTick, defer);
28 | defer(done);
29 | });
30 |
31 | it('requested 0.12.1', function (done) {
32 | const defer = later('0.12.1');
33 | assert.strictEqual(process.nextTick, defer);
34 | defer(done);
35 | });
36 |
37 | it('requested 0.10.x', function (done) {
38 | const defer = later('0.10.1000');
39 | assert.strictEqual(setImmediate, defer);
40 | defer(done);
41 | });
42 |
43 | it('runtime 0.12.0', function (done) {
44 | const nodejsVersion = later.nodejsVersion;
45 | later.nodejsVersion = '0.12.0';
46 |
47 | const defer = later();
48 | later.nodejsVersion = nodejsVersion;
49 |
50 | assert.strictEqual(process.nextTick, defer);
51 | defer(done);
52 | });
53 |
54 | it('runtime 0.12.1', function (done) {
55 | const nodejsVersion = later.nodejsVersion;
56 | later.nodejsVersion = '0.12.1';
57 |
58 | const defer = later();
59 | later.nodejsVersion = nodejsVersion;
60 |
61 | assert.strictEqual(process.nextTick, defer);
62 | defer(done);
63 | });
64 |
65 | it('runtime 0.10.x', function (done) {
66 | const nodejsVersion = later.nodejsVersion;
67 | later.nodejsVersion = '0.10.1000';
68 |
69 | const defer = later();
70 | later.nodejsVersion = nodejsVersion;
71 |
72 | assert.strictEqual(setImmediate, defer);
73 | defer(done);
74 | });
75 | });
76 | });
77 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # node-later
2 |
3 | [](https://www.npmjs.org/package/node-later) [](https://github.com/sagiegurari/node-later/actions) [](https://coveralls.io/r/sagiegurari/node-later) [](https://snyk.io/test/github/sagiegurari/node-later) [](http://inch-ci.org/github/sagiegurari/node-later) [](https://github.com/sagiegurari/node-later/blob/master/LICENSE) [](https://www.npmjs.org/package/node-later)
4 |
5 | > Defer function calls to the start of the next cycle.
6 |
7 | * [Overview](#overview)
8 | * [Usage](#usage)
9 | * [Installation](#installation)
10 | * [API Documentation](docs/api.md)
11 | * [Contributing](.github/CONTRIBUTING.md)
12 | * [Release History](#history)
13 | * [License](#license)
14 |
15 |
16 | ## Overview
17 | Invoke functions in the next cycle.