{"id":18573352,"url":"https://github.com/szchenghuang/debounce-async","last_synced_at":"2025-04-10T07:31:58.669Z","repository":{"id":57225346,"uuid":"68685423","full_name":"szchenghuang/debounce-async","owner":"szchenghuang","description":"A debounce function that delays invoking  asynchronous functions.","archived":false,"fork":false,"pushed_at":"2020-09-30T03:31:08.000Z","size":12,"stargazers_count":25,"open_issues_count":5,"forks_count":6,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-29T06:35:17.692Z","etag":null,"topics":["async-await","debounce","promise"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/szchenghuang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-20T07:24:19.000Z","updated_at":"2024-03-26T10:02:02.000Z","dependencies_parsed_at":"2022-08-24T10:40:19.768Z","dependency_job_id":null,"html_url":"https://github.com/szchenghuang/debounce-async","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szchenghuang%2Fdebounce-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szchenghuang%2Fdebounce-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szchenghuang%2Fdebounce-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szchenghuang%2Fdebounce-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/szchenghuang","download_url":"https://codeload.github.com/szchenghuang/debounce-async/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247949590,"owners_count":21023352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async-await","debounce","promise"],"created_at":"2024-11-06T23:09:03.213Z","updated_at":"2025-04-10T07:31:58.408Z","avatar_url":"https://github.com/szchenghuang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# debounce-async #\n\n[![Build Status][travis_img]][travis_site]\n[![NPM Package][npm_img]][npm_site]\n[![Dependency status][david_img]][david_site]\n[![devDependency status][david_dev_img]][david_dev_site]\n\n[![NPM][nodei_img]][nodei_site]\n\nA debounced function that delays invoking asynchronous functions.\n\n## Preliminaries ##\n\nA debounced function groups sequential calls to a function within a period. Only\nthe last call in the group is executed. The others are simply ignored with\nrejections as if no calls to them ever happened.\n\nSay `d` is a debounced function of `f`, `var d = debounce(f, 1000);`, where f is\nan asynchronous function that returns a promise being resolved in 400ms since it\ngets called. Below is a depiction of a sequence of calls to `d`.\n\n```\nseconds elapsed    0         1         2         3         4\nd called           - d d d d d - - - - - d d d d - - d - - - - -\n                     x x x x |           x x x x     |\nf called                     +-------\u003e f             +-------\u003e f\n                                       |                       |\npromise resolved                       +-\u003e *                   +-\u003e *\n```\n\n`d` denotes a call to function `d`, `f` denotes a call to function `f`, and `*`\ndenotes a promise returned by `f` is resolved. `x` denotes a call to `d` returns\na rejected promise.\n\n## Installation ##\n\n```sh\nnpm install debounce-async --save\n```\n\n## Usage ##\n\n```js\nvar debounce = require( 'debounce-async' );\n\n/**\n  * debounce(func, [wait=0], [options={}])\n  *\n  * @param {Function} func The function to debounce.\n  * @param {number} [wait=0] The number of milliseconds to delay.\n  * @param {Object} [options={}] The options object.\n  * @param {boolean} [options.leading=false] Specify invoking on the leading edge of the timeout.\n  * @param {cancelObj} [options.cancelObj='canceled'] Specify the error object to be rejected.\n  * @returns {Function} Returns the new debounced function.\n  */\n```\n\nThis package aims at maintaining the same signature of the `debounce` function from `lodash`.\nPlease report if there is discrenency.\n\n## Example ##\n\n### Promise ###\n\n```js\nvar debounce = require( 'debounce-async' );\n\nvar f = value =\u003e new Promise( resolve =\u003e setTimeout( () =\u003e resolve( value ), 50 ) );\nvar debounced = debounce( f, 100 );\n\nvar promises = [ 'foo', 'bar' ].map( debounced );\npromises.forEach( promise =\u003e {\n  promise\n    .then( res =\u003e console.log( 'resolved:', res ) )\n    .catch( err =\u003e console.log( 'rejected:', err ) )\n});\n\n// Output:\n// rejected: canceled\n// resolved: bar\n```\n\nIn the example above, `f` is an asynchronous function which returns a promise.\nThe promise is resolved with the input after 50ms. `debounced` is a debounced\nfunction of `f` with a delay of 100ms.\n\nThe debounced function is called twice consecutively by the callback of\n`Array.proptotype.map`, with `'foo'` and `'bar'` being the input value\nrespectively. The two returned promises are next fullfilled by printing the\nresolved result or rejected error on the console.\n\nThis snippet results in the given output. The first promise was rejected while\nthe second one was resolved. It is because the second call comes before the delay\nof 100ms since the first call fired.\n\n### async/await ###\n\nSame thing when it comes to asynchronous ES7 async/await functions. Take the\nprior example and transform the `f` into an ES7 async function.\n\n```js\nvar f = async value =\u003e await new Promise( resolve =\u003e setTimeout( () =\u003e resolve( value ), 50 ) );\n```\n\nSame output can be expected.\n\n## Test ##\n\n```js\nnpm test\n\n```\n## License ##\n\nMIT. See [LICENSE.md][license] for details.\n\n[travis_img]: https://travis-ci.org/szchenghuang/debounce-async.svg?branch=master\n[travis_site]: https://travis-ci.org/szchenghuang/debounce-async\n[npm_img]: https://img.shields.io/npm/v/debounce-async.svg\n[npm_site]: https://www.npmjs.org/package/debounce-async\n[nodei_img]: https://nodei.co/npm/debounce-promise.png\n[nodei_site]: https://nodei.co/npm/debounce-async\n[david_img]: https://david-dm.org/szchenghuang/debounce-async/status.svg\n[david_site]: https://david-dm.org/szchenghuang/debounce-async/\n[david_dev_img]: https://david-dm.org/szchenghuang/debounce-async/dev-status.svg\n[david_dev_site]: https://david-dm.org/szchenghuang/debounce-async/?type=dev\n[license]: http://github.com/szchenghuang/debounce-async/blob/master/LICENSE.md\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszchenghuang%2Fdebounce-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fszchenghuang%2Fdebounce-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszchenghuang%2Fdebounce-async/lists"}