{"id":18368234,"url":"https://github.com/thinkjs/think-debounce","last_synced_at":"2025-04-06T17:31:40.707Z","repository":{"id":65412376,"uuid":"65513534","full_name":"thinkjs/think-debounce","owner":"thinkjs","description":"think debounce","archived":false,"fork":false,"pushed_at":"2018-09-04T02:19:37.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-29T09:43:10.862Z","etag":null,"topics":["thinkjs","thinkjs3"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":false,"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/thinkjs.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-08-12T01:34:07.000Z","updated_at":"2018-09-04T02:19:38.000Z","dependencies_parsed_at":"2023-01-22T07:55:25.370Z","dependency_job_id":null,"html_url":"https://github.com/thinkjs/think-debounce","commit_stats":null,"previous_names":["thinkjs/think-await"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-debounce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-debounce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-debounce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkjs%2Fthink-debounce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thinkjs","download_url":"https://codeload.github.com/thinkjs/think-debounce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247522345,"owners_count":20952528,"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":["thinkjs","thinkjs3"],"created_at":"2024-11-05T23:25:14.909Z","updated_at":"2025-04-06T17:31:40.392Z","avatar_url":"https://github.com/thinkjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# think-debounce\n[![Build Status](https://img.shields.io/travis/thinkjs/think-debounce/master.svg?style=flat-square)](https://travis-ci.org/thinkjs/think-debounce)\n[![Coverage Status](https://img.shields.io/coveralls/thinkjs/think-debounce/master.svg?style=flat-square)](https://coveralls.io/github/thinkjs/think-debounce?branch=master)\n[![npm](https://img.shields.io/npm/v/think-debounce.svg?colorB=brightgreen\u0026style=flat-square)](https://www.npmjs.com/package/think-debounce)\n\n`think-debounce` runs a time-consuming operation. The operation may be called several times concurrently, but within `think-debounce`, it will only be run once before it's finished.\n\nIn particular, You can use `think-debounce` to avoid duplicate requests for a remote API.\n\n## Syntax\n\n```js\nimport Debounce from 'think-debounce';\nconst instance = new Debounce();\ninstance.debounce(key, callback);\n```\n\n- `key` {String} the identity of the operation.\n- `callback` {Function} the function which contains the operation and returns a Promise object.\n- return {Object} a Promise object.\n\n## Usage\n\nTake reading a local file for an example:\n\n```js\nimport Debounce from 'think-debounce';\nimport fs from 'fs';\n\nlet instance = new Debounce();\nlet readTimes = 0;\nlet awaitKey = 'readMyFile';\nlet filePath = '../my/file/path';\nlet readMyFileCallback = () =\u003e {\n  return new Promise((resolve, reject) =\u003e {\n    // even if `readMyFile` operation is called several times,\n    // `readMyFileCallback` will only be run once before it's finished.\n    readTimes ++;\n    fs.readFile(filePath, {encoding: 'utf8'}, (err, data) =\u003e {\n      if(err) reject(err);\n      resolve(data);\n    });\n  });\n}\n\nlet promise1 = instance.debounce(awaitKey, readMyFileCallback);\nlet promise2 = instance.debounce(awaitKey, readMyFileCallback);\n\nreturn Promise.all([promise1, promise2]).then(values =\u003e {\n  console.log(readTimes); // 1\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkjs%2Fthink-debounce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthinkjs%2Fthink-debounce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkjs%2Fthink-debounce/lists"}