{"id":21040990,"url":"https://github.com/chfoo/thenshim","last_synced_at":"2025-06-23T20:07:31.406Z","repository":{"id":145909831,"uuid":"200118737","full_name":"chfoo/thenshim","owner":"chfoo","description":"Adapter/shim for cross-target JavaScript-style ('thenable') promises Haxe library ","archived":false,"fork":false,"pushed_at":"2024-12-11T22:37:49.000Z","size":41,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T21:24:16.918Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Haxe","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/chfoo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-08-01T21:02:30.000Z","updated_at":"2024-12-11T22:34:13.000Z","dependencies_parsed_at":"2024-02-13T07:00:15.631Z","dependency_job_id":null,"html_url":"https://github.com/chfoo/thenshim","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/chfoo/thenshim","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chfoo%2Fthenshim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chfoo%2Fthenshim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chfoo%2Fthenshim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chfoo%2Fthenshim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chfoo","download_url":"https://codeload.github.com/chfoo/thenshim/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chfoo%2Fthenshim/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261548735,"owners_count":23175495,"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-11-19T13:49:36.664Z","updated_at":"2025-06-23T20:07:31.386Z","avatar_url":"https://github.com/chfoo.png","language":"Haxe","readme":"# Thenshim\n\nThenshim is an adapter/shim for cross-target JavaScript-style (\"thenable\") promises for Haxe. On the JS target, it uses the native Promise object. On other targets, a fallback implementation is used. The fallback implementation is [Promises/A+](https://promisesaplus.com/) compliant.\n\nThenshim is intended for applications or libraries that want to use JavaScript-style promises across targets. For more feature-rich asynchronous functionality, such as cancellation, consider using a different library. (You can search on [Haxelib](https://lib.haxe.org/) to find various libraries that best suit your project.)\n\n## Getting started\n\nRequires:\n\n* Haxe 4.0\n\nThe library can be installed using haxelib:\n\n    haxelib install thenshim\n\nOr the latest from the GitHub repository:\n\n    haxelib git thenshim https://github.com/chfoo/thenshim\n\n### Concepts\n\nIf you never used a JavaScript promise or something similar, it is highly recommended to read an introduction such as [this one on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises).\n\nThe `Promise` abstract is an abstract over `js.lib.Promise` on JS and `FallbackPromise` on other targets.\n\nThe `FallbackPromise` class implements the `Thenable` interface. The `Thenable` interface can be used to specify an alternative implementation if needed.\n\nThe `then` method is slightly more restrictive than the JS method. It has two parameters with the following signatures:\n\n* onFulfilled: `T-\u003ePromise\u003cR\u003e`, `T-\u003eThenable\u003cR\u003e`, or `T-\u003eR`\n* onRejected (optional): `Any-\u003ePromise\u003cR\u003e`, `T-\u003eThenable\u003cR\u003e`, or `T-\u003eR`\n\nUnlike JS-style promises which can return parameter types `T`, `R1`, or `R2`, this library's `then` method only allows `R` for straightforward parameter typing.\n\n### Using promises\n\nTo use a promise, pass a callback function to the `then` method. The `onFulfilled` callback is called with a value of type `T` when the operation successfully completes. The `onRejected` callback is called with a reason `Any` when there is an exception or error. Both callbacks returns a new promise or value of type `R`.\n\nExample of using `then`:\n\n```haxe\n// Schedule and execute an asynchronous operation\nvar promise:Promise\u003cString\u003e = someAsyncOperation();\n\npromise.then((result:String) -\u003e {\n    // Once the asynchronous operation completes, print the result\n    trace(result);\n\n    // Return a value as demo for chaining\n    return result.length;\n}).then((result:Int) -\u003e {\n    trace(result);\n\n    // To end the promise chain, return a dummy value\n    return true;\n});\n```\n\n### Creating promises\n\n#### New Promise\n\nA promise can be created to wrap an operation into a JavaScript-style promise:\n\n```haxe\nvar promise = new Promise((resolve, reject) -\u003e {\n    // Do something\n\n    // If it was successful:\n    resolve(\"abc\");\n\n    // Otherwise, reject with an error:\n    reject(\"error\");\n});\n```\n\n#### resolve()\n\n`Promise.resolve()` is a special method:\n\n* When given a value, it returns a settled promise that is fulfilled with the given value (as a convenience function).\n* When given a `Promise`, it returns the promise itself.\n* When given a `Thenable`, it returns a new promise that reflects the `Thenable`'s state.\n\n#### reject()\n\n`Promise.reject()` is a convenience method returns a settled promise that is already rejected with the given reason.\n\n### Task scheduler\n\nCallbacks, in practice, are called asynchronously as required by specifications. The task scheduler function for the fallback promise implementation can be changed like so:\n\n```haxe\nvar promiseFactory = cast(Promise.factory, FallbackPromiseFactory);\npromiseFactory.scheduler.addNext = myScheduleCallbackToEventLoopFunction;\n```\n\nBy default, the task is called synchronously. You will need to implement and integrate the functionality into your event loop.\n\nOn the JS target, `setImmediate` or `setTimeout` will be used for task scheduling. This implementation is intended for testing, as native promises should be used instead.\n\n### Additional methods\n\nAdditional JavaScript-style promise methods are provided in `PromiseTools`. It is a static method extension class that can be used with the `using` keyword.\n\n### Further reading\n\nAPI docs: https://chfoo.github.io/thenshim/api/\n\n## Contributing\n\nIf there is a bug or improvement, please file an issue or pull request on the GitHub repository.\n\n## Copyright\n\nCopyright 2019 Christopher Foo. Licensed under MIT.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchfoo%2Fthenshim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchfoo%2Fthenshim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchfoo%2Fthenshim/lists"}