{"id":16189657,"url":"https://github.com/stuk/promise-me","last_synced_at":"2025-09-24T10:31:43.353Z","repository":{"id":5840916,"uuid":"7056986","full_name":"Stuk/promise-me","owner":"Stuk","description":"Code transformer to change Node-style callbacks into promises. ","archived":false,"fork":false,"pushed_at":"2017-03-16T13:36:27.000Z","size":1252,"stargazers_count":46,"open_issues_count":5,"forks_count":6,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-01-07T18:12:09.071Z","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":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Stuk.png","metadata":{"files":{"readme":"README.markdown","changelog":"CHANGES.markdown","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":"2012-12-07T17:22:53.000Z","updated_at":"2023-04-13T14:26:18.000Z","dependencies_parsed_at":"2022-08-06T19:00:55.575Z","dependency_job_id":null,"html_url":"https://github.com/Stuk/promise-me","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stuk%2Fpromise-me","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stuk%2Fpromise-me/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stuk%2Fpromise-me/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stuk%2Fpromise-me/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Stuk","download_url":"https://codeload.github.com/Stuk/promise-me/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234074376,"owners_count":18775341,"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-10T07:36:17.591Z","updated_at":"2025-09-24T10:31:38.080Z","avatar_url":"https://github.com/Stuk.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Promise Me [![Build Status](https://secure.travis-ci.org/Stuk/promise-me.png?branch=master)](https://travis-ci.org/Stuk/promise-me)\n==========\n\nPromise Me helps you move your code from using callbacks to using [promises](http://wiki.commonjs.org/wiki/Promises/A), for example through [Q](https://github.com/kriskowal/q), [RSVP.js](https://github.com/tildeio/rsvp.js) or [when.js](https://github.com/cujojs/when).\n\nIt parses your code and then manipulates the AST to transform the callbacks into calls to `then()`, including a rejection handler if you handle the original callback error. Think of it as a slightly smarter find-and-replace. It will probably break your code and require you to fix it.\n\nTry the [live demo](http://stuk.github.com/promise-me/)!\n\nInstallation and usage\n----------------------\n\n```bash\n$ npm install -g promise-me\n$ promise-me script.js\n```\n\n### API\n\n```javascript\nvar promiseMe = require(\"promise-me\");\n\nvar before = \"...\";\nvar after = promiseMe.convert(before, options);\n```\n\n#### promiseMe.convert(code, options)\n\nConvert the given code to use promises.\n\n * `{string} code`    String containing Javascript code.\n * `{Object} options` Options for generation.\n * `{Object} options.parse`       Options for `esprima.parse`. See http://esprima.org/doc/ .\n * `{Object} options.generate`    Options for `escodegen.generate`. See https://github.com/Constellation/escodegen/wiki/API .\n * `{Function} options.matcher`   A function of form `(node) =\u003e boolean`. Must accept any type of node and return true if it should be transformed into `then`, using the replacer, and false if not. See https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API for node types.\n * `{Function} options.replacer`  A function of form `(node) =\u003e Node`. Must accept any type of node and return a new node to replace it that uses `then` instead of a callback. Will only get called if options.matcher returns true. See https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API for node types.\n * `{Function} options.flattener`  A function of form `(node) =\u003e Node`. Must accept any type of node, and return either return the original node, or a new node with the `then` calls flattened.\n * `{Function} log`   Function to call with log messages.\n * Returns `{string}`         The Javascript code with callbacks replaced with `.then()` functions.\n\n\nExamples\n--------\n\n### Simple callback\n\nBefore:\n```javascript\ngetDetails(\"Bob\", function (err, details) {\n    console.log(details)\n});\n```\n\nAfter:\n```javascript\ngetDetails('Bob').then(function (details) {\n    console.log(details);\n});\n```\n\n### Error handling\n\nBefore:\n```javascript\ngetDetails(\"Bob\", function (err, details) {\n    if (err) {\n        console.error(err);\n        return;\n    }\n    console.log(details)\n});\n```\n\nAfter:\n```javascript\ngetDetails('Bob').then(function (details) {\n    console.log(details);\n}, function (err) {\n    console.error(err);\n    return;\n});\n```\n\n### Nested callbacks\n\nBefore:\n```javascript\ngetDetails(\"Bob\", function (err, details) {\n    getLongLat(details.address, details.country, function(err, longLat) {\n        getNearbyBars(longLat, function(err, bars) {\n            console.log(\"Your nearest bar is: \" + bars[0]);\n        });\n    });\n});\n```\n\nAfter:\n```javascript\ngetDetails('Bob').then(function (details) {\n    return getLongLat(details.address, details.country);\n}).then(function (longLat) {\n    return getNearbyBars(longLat);\n}).then(function (bars) {\n    console.log('Your nearest bar is: ' + bars[0]);\n});\n```\n\n### Captured variables\n\nBefore:\n```javascript\ngetDetails(\"Bob\", function (err, details) {\n    getLongLat(details.address, details.country, function(err, longLat) {\n        getNearbyBars(longLat, function(err, bars) {\n            // Note the captured `details` variable\n            console.log(\"The closest bar to \" + details.address + \" is: \" + bars[0]);\n        });\n    });\n});\n```\n\nAfter:\n```javascript\ngetDetails(\"Bob\").then(function (details) {\n    getLongLat(details.address, details.country).then(function (longLat) {\n        return getNearbyBars(longLat);\n    }).then(function (bars) {\n        // Note the captured `details` variable\n        console.log(\"The closest bar to \" + details.address + \" is: \" + bars[0]);\n    });\n});\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstuk%2Fpromise-me","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstuk%2Fpromise-me","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstuk%2Fpromise-me/lists"}