{"id":16143570,"url":"https://github.com/isaacs/once","last_synced_at":"2025-05-15T09:00:21.150Z","repository":{"id":4280588,"uuid":"5409660","full_name":"isaacs/once","owner":"isaacs","description":"Run a function exactly one time","archived":false,"fork":false,"pushed_at":"2023-10-10T17:28:46.000Z","size":77,"stargazers_count":222,"open_issues_count":2,"forks_count":29,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-30T19:03:26.400Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/isaacs.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,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":["isaacs"]}},"created_at":"2012-08-14T07:25:37.000Z","updated_at":"2024-08-29T18:11:22.000Z","dependencies_parsed_at":"2024-02-26T01:49:28.107Z","dependency_job_id":"2d87b5cd-8cc6-4b94-8a8d-a5136612b0a4","html_url":"https://github.com/isaacs/once","commit_stats":{"total_commits":21,"total_committers":7,"mean_commits":3.0,"dds":0.2857142857142857,"last_synced_commit":"0e004fada2c3f9a3a6065c6aae52c9d4b074fc65"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaacs%2Fonce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaacs%2Fonce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaacs%2Fonce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaacs%2Fonce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/isaacs","download_url":"https://codeload.github.com/isaacs/once/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251932975,"owners_count":21667265,"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":[],"created_at":"2024-10-10T00:09:34.712Z","updated_at":"2025-05-15T09:00:21.070Z","avatar_url":"https://github.com/isaacs.png","language":"JavaScript","readme":"# once\n\nOnly call a function once.\n\n## usage\n\n```javascript\nvar once = require('once')\n\nfunction load (file, cb) {\n  cb = once(cb)\n  loader.load('file')\n  loader.once('load', cb)\n  loader.once('error', cb)\n}\n```\n\nOr add to the Function.prototype in a responsible way:\n\n```javascript\n// only has to be done once\nrequire('once').proto()\n\nfunction load (file, cb) {\n  cb = cb.once()\n  loader.load('file')\n  loader.once('load', cb)\n  loader.once('error', cb)\n}\n```\n\nIronically, the prototype feature makes this module twice as\ncomplicated as necessary.\n\nTo check whether your function has been called, use `fn.called`. Once the\nfunction is called for the first time the return value of the original\nfunction is saved in `fn.value` and subsequent calls will continue to\nreturn this value.\n\n```javascript\nvar once = require('once')\n\nfunction load (cb) {\n  cb = once(cb)\n  var stream = createStream()\n  stream.once('data', cb)\n  stream.once('end', function () {\n    if (!cb.called) cb(new Error('not found'))\n  })\n}\n```\n\n## `once.strict(func)`\n\nThrow an error if the function is called twice.\n\nSome functions are expected to be called only once. Using `once` for them would\npotentially hide logical errors.\n\nIn the example below, the `greet` function has to call the callback only once:\n\n```javascript\nfunction greet (name, cb) {\n  // return is missing from the if statement\n  // when no name is passed, the callback is called twice\n  if (!name) cb('Hello anonymous')\n  cb('Hello ' + name)\n}\n\nfunction log (msg) {\n  console.log(msg)\n}\n\n// this will print 'Hello anonymous' but the logical error will be missed\ngreet(null, once(msg))\n\n// once.strict will print 'Hello anonymous' and throw an error when the callback will be called the second time\ngreet(null, once.strict(msg))\n```\n","funding_links":["https://github.com/sponsors/isaacs"],"categories":["模块","Modules"],"sub_categories":["函数","Function"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisaacs%2Fonce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fisaacs%2Fonce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisaacs%2Fonce/lists"}