{"id":18032345,"url":"https://github.com/dmitriz/promises-vs-callbacks","last_synced_at":"2025-04-04T21:45:24.627Z","repository":{"id":146747426,"uuid":"226448338","full_name":"dmitriz/promises-vs-callbacks","owner":"dmitriz","description":"JS promise is not a replacement for callbacks","archived":false,"fork":false,"pushed_at":"2023-12-15T11:53:45.000Z","size":4,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-10T06:30:49.919Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"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/dmitriz.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,"publiccode":null,"codemeta":null}},"created_at":"2019-12-07T03:13:58.000Z","updated_at":"2020-07-10T02:21:01.000Z","dependencies_parsed_at":"2024-10-30T10:23:13.272Z","dependency_job_id":null,"html_url":"https://github.com/dmitriz/promises-vs-callbacks","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/dmitriz%2Fpromises-vs-callbacks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmitriz%2Fpromises-vs-callbacks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmitriz%2Fpromises-vs-callbacks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmitriz%2Fpromises-vs-callbacks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmitriz","download_url":"https://codeload.github.com/dmitriz/promises-vs-callbacks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256080,"owners_count":20909240,"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-30T10:13:05.841Z","updated_at":"2025-04-04T21:45:24.602Z","avatar_url":"https://github.com/dmitriz.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# promises-vs-callbacks\nJS promise is not a replacement for callbacks\n\n## Don't confuse callbacks as clean and powerful pattern with Node API\n\nThe continuation-passing-style behind callbacks is one of the oldest and most fundamental functional patterns used across most programming languages. It is the simplest way to write async code without any extra stuff other than functions. Callback style provides a unified functional way of writing database interactions, HTTP requests, streams, websockets, event listeners etc. \nIn JavaScript, where functions are variadic by design, [this pattern can be made yet more powerful with functions accepting any number of callbacks](https://github.com/dmitriz/cpsfy/blob/master/DOCUMENTATION.md#variadic-input-and-output). Finally, a clean functional composition for callback-style functions is [easily achievable with few simple convenience operators](https://github.com/dmitriz/cpsfy).\n\n\n## The so-called \"callback hell\" is an issue with Node API, not callbacks\n\nThe Node API is a very specific way of using callbacks. Node API callbacks are mixed along with other arguments with no clean separation between in- and outgoing data. Node API callbacks receive the error argument ahead of others making it less convenient to defer the error handling. This lack of convenience is very specific to that particular style though that can be [easily fixed](https://github.com/dmitriz/cpsfy/blob/master/DOCUMENTATION.md#what-about-callback-hell).\n\n\n## JS Promises are really a collection of opinionated convenience methods\n\nThere is no question about promises being convenient in many simple situations. Making them perfectly legitimate to use in those cases. There is also no question about their [numerous problems](https://medium.com/@avaq/broken-promises-2ae92780f33). Making them unsuitable or dangerous for many other cases and a bad paradigm to advocate for when not accompanied by clear warnings about those dangers and limitations. Promises aren't suitable or too cumbersome for streams, websockets, event listeners or anything more advanced than single request-response. The fact that promise is more of a convenience choice than something fundamental is illustrated by their lacking presence anywhere beyond JavaScript and their [lacking conformity to general functional paradigms](https://stackoverflow.com/a/50173415/1614973). And even in JavaScript, it is illustrated by the need of further syntax improvements such as `async/await`.\n\n\n## We must acknowledge promise was a wrong choice as primitive and move on\n\nThe fact that promises are simple and convenient in many simple cases doesn't make them a good choice as primitives replacing callbacks:\n\nhttps://github.com/promises-aplus/promises-spec/issues/94#issuecomment-553767177\n\u003e 1. The things done wrong here are fundamental to programming/computation itself, so there will never be a time when it is not wrong. And probably never be a time when it doesn't adversely effect programming in Javascript/Typescript.\n\u003e 2. Those who think the conclusions here are OK will never search it up. This thread is kept alive by those who encountered the wrongness and searched the web for what went wrong.\n\nCreating primitives that are more fundamental, powerful, composable and flexible than promises, that are universal accross languages and backed by science is [not hard](https://github.com/dmitriz/cpsfy). There is no excuse for getting stuck with promises. We need to move on.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmitriz%2Fpromises-vs-callbacks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmitriz%2Fpromises-vs-callbacks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmitriz%2Fpromises-vs-callbacks/lists"}