{"id":18389555,"url":"https://github.com/anseki/dual-api","last_synced_at":"2025-06-30T14:08:39.333Z","repository":{"id":57216887,"uuid":"52439845","full_name":"anseki/dual-api","owner":"anseki","description":"Make your module implement dual APIs, callback API and thenable (Promise) API.","archived":false,"fork":false,"pushed_at":"2022-08-19T03:00:02.000Z","size":6,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-12T08:50:29.004Z","etag":null,"topics":["api","callback","callback-api","dual","dual-apis","promise","reject","resolve","then","thenable"],"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/anseki.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-02-24T12:16:03.000Z","updated_at":"2022-08-19T03:00:04.000Z","dependencies_parsed_at":"2022-08-28T21:40:18.046Z","dependency_job_id":null,"html_url":"https://github.com/anseki/dual-api","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/anseki/dual-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anseki%2Fdual-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anseki%2Fdual-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anseki%2Fdual-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anseki%2Fdual-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anseki","download_url":"https://codeload.github.com/anseki/dual-api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anseki%2Fdual-api/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259747231,"owners_count":22905313,"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":["api","callback","callback-api","dual","dual-apis","promise","reject","resolve","then","thenable"],"created_at":"2024-11-06T01:43:43.969Z","updated_at":"2025-06-30T14:08:39.299Z","avatar_url":"https://github.com/anseki.png","language":"JavaScript","readme":"# dualAPI\n\n[![npm](https://img.shields.io/npm/v/dual-api.svg)](https://www.npmjs.com/package/dual-api) [![GitHub issues](https://img.shields.io/github/issues/anseki/dual-api.svg)](https://github.com/anseki/dual-api/issues) [![dependencies](https://img.shields.io/badge/dependencies-No%20dependency-brightgreen.svg)](package.json) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\nMake your module implement dual APIs.  \nWhen the method of your module is called, if a callback Function is appended to the arguments, the method works as callback API. Otherwise the method returns a thenable [`Promise`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) instance.\n\nFor example:  \nThis `egg` module exports two methods.  \nThe `egg.fry` method returns `'fried egg'` if it is called with `'oil'`, otherwise it returns `[Error: burnt egg]`. The `egg.boil` method returns `'boiled egg'` if it is called with `'water'`, otherwise it returns `[Error: broken egg]`. These are the same as a function that is passed to `Promise` constructor.  \n**After** setting up `exports`, call `require('dual-api')`.\n\n```js\n// `egg` module\n\nfunction fry(resolve, reject, doWith) {\n  console.log(`An egg is fried with ${doWith || 'nothing'}.`);\n  setTimeout(() =\u003e {\n    if (doWith === 'oil') {\n      resolve('fried egg');\n    } else {\n      reject(new Error('burnt egg'));\n    }\n  }, 3000);\n}\n\nfunction boil(resolve, reject, doWith) {\n  console.log(`An egg is boiled with ${doWith || 'nothing'}.`);\n  setTimeout(() =\u003e {\n    if (doWith === 'water') {\n      resolve('boiled egg');\n    } else {\n      reject(new Error('broken egg'));\n    }\n  }, 3000);\n}\n\nexports.fry = fry;\nexports.boil = boil;\n\nrequire('dual-api');\n```\n\nA code that uses `egg` module:\n\n```js\nconst egg = require('egg');\n\n// Call methods as callback API:\n\negg.fry('oil', function(error, returned) {\n  if (error) {\n    console.error(error);\n  } else {\n    console.log(`Got a ${returned}.`);\n  }\n});\n\negg.boil('water', function(error, returned) {\n  if (error) {\n    console.error(error);\n  } else {\n    console.log(`Got a ${returned}.`);\n  }\n});\n\n// Call methods as thenable API:\n\negg.fry('oil')\n  .then(returned =\u003e { console.log(`Got a ${returned}.`); })\n  .catch(error =\u003e { console.error(error); });\n\negg.boil('water')\n  .then(returned =\u003e { console.log(`Got a ${returned}.`); })\n  .catch(error =\u003e { console.error(error); });\n```\n\nResults:\n\n```\nAn egg is fried with oil.\nGot a fried egg.\nAn egg is boiled with water.\nGot a boiled egg.\nAn egg is fried with oil.\nGot a fried egg.\nAn egg is boiled with water.\nGot a boiled egg.\n```\n\nA single method that is set to `module.exports` is also supported.  \nFor example, `egg-fly` module that exports only `fry` method:\n\n```js\n// `egg-fly` module\n\n// There is `fry` function.\n\nmodule.exports = fry;\nrequire('dual-api');\n```\n\nA code that uses `egg-fly` module:\n\n```js\nconst eggFly = require('egg-fly');\n\n// Call method as callback API:\n\neggFly('oil', function(error, returned) {\n  if (error) {\n    console.error(error);\n  } else {\n    console.log(`Got a ${returned}.`);\n  }\n});\n\n// Call method as thenable API:\n\neggFly('oil')\n  .then(returned =\u003e { console.log(`Got a ${returned}.`); })\n  .catch(error =\u003e { console.error(error); });\n```\n\nBy default, all exported methods implement dual APIs.  \nYou can specify methods that implement dual APIs by using `exports._dualApi_methodNames` array.  \nFor example, the module exports `fry`, `boil` and `beatUp` methods, and only `boil` and `beatUp` methods implement dual APIs.\n\n```js\n// `egg` module\n\n// There are `fry`, `boil` and `beatUp` functions.\n\nexports.fry = fry;\nexports.boil = boil;\nexports.beatUp = beatUp;\n\nexports._dualApi_methodNames = ['boil', `beatUp`];\n\nrequire('dual-api');\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanseki%2Fdual-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanseki%2Fdual-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanseki%2Fdual-api/lists"}