{"id":19145520,"url":"https://github.com/romanpunia/aspromise","last_synced_at":"2025-09-04T05:08:59.652Z","repository":{"id":157366335,"uuid":"633417028","full_name":"romanpunia/aspromise","owner":"romanpunia","description":"Promise for AngelScript","archived":false,"fork":false,"pushed_at":"2024-03-01T06:32:59.000Z","size":2241,"stargazers_count":16,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-19T17:17:09.603Z","etag":null,"topics":["angelscript","async","await","promise"],"latest_commit_sha":null,"homepage":"","language":"C++","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/romanpunia.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":"2023-04-27T13:01:28.000Z","updated_at":"2025-04-09T10:25:01.000Z","dependencies_parsed_at":"2024-11-09T07:51:46.240Z","dependency_job_id":null,"html_url":"https://github.com/romanpunia/aspromise","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/romanpunia%2Faspromise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanpunia%2Faspromise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanpunia%2Faspromise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanpunia%2Faspromise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/romanpunia","download_url":"https://codeload.github.com/romanpunia/aspromise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252797725,"owners_count":21805760,"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":["angelscript","async","await","promise"],"created_at":"2024-11-09T07:40:45.077Z","updated_at":"2025-05-07T01:40:54.557Z","avatar_url":"https://github.com/romanpunia.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"## About\r\nA header-only implementation of promise concept for AngelScript library. Actual implementation is in __promise.hpp__ file, see example usage (examples directory): is in __promises.cpp__ and __promises.as__\r\n\r\n## Usage\r\nDrag and drop __promise.hpp__ somewhere into your project.\r\n\r\n## Example usage with AngelScript\r\nPromise creation\r\n```as\r\n    promise\u003cint\u003e@ result = promise\u003cint\u003e();\r\n    // Use promise_v for promise\u003cvoid\u003e\r\n```\r\n\r\nPromise settlement\r\n```cpp\r\n    result.wrap(10);\r\n```\r\n\r\nPromise awaiting using coroutines\r\n```cpp\r\n    int number_awaited = co_await result;\r\n    int number_unwrapped = result.yield().unwrap();\r\n```\r\n\r\nPromise awaiting using callbacks\r\n```cpp\r\n    result.when(function(wrapped_number)\r\n    {\r\n        int number_unwrapped = wrapped_number.unwrap();\r\n    });\r\n```\r\n\r\n## Example usage with C++\r\nPromise creation\r\n```cpp\r\n    AsBasicPromise\u003cExecutor\u003e* Result = AsBasicPromise\u003cExecutor\u003e::Create();\r\n    /*\r\n        Built-in implementations:\r\n            AsDirectPromise = thread that resolves the promise continues script execution,\r\n            AsReactivePromise = thread that resolves notifies the initiator\r\n    */\r\n```\r\n\r\nPromise settlement\r\n```cpp\r\n    int32_t Number = 10;\r\n    Result-\u003eStore(\u0026Number, asTYPEID_INT32);\r\n```\r\n\r\nPromise awaiting using wait\r\n```cpp\r\n    int32_t Number;\r\n    Result-\u003eWaitIf()-\u003eRetrieve(\u0026Number, asTYPEID_INT32);\r\n```\r\n\r\nPromise awaiting using callbacks\r\n```cpp\r\n    Result-\u003eWhen([](AsBasicPromise\u003cExecutor\u003e* Result)\r\n    {\r\n        int32_t Number;\r\n        Result-\u003eRetrieve(\u0026Number, asTYPEID_INT32);\r\n    });\r\n```\r\n\r\n## Details\r\nPromise object it self is measurably lightweight, it follows guarantees provided by **\\\u003cany\\\u003e** class.\r\nFrom design standpoint it provides pretty simple but effective API: **get/set/pending** functions.\r\nImplementation works in a way that allows one to never block for waiting. This could be used for\r\neffective task processing in concurrent environments. AngelScript implements coroutines concept which\r\nis highly utilized by this promise interface.\r\n\r\nAngelScript engine will work with class as with GC watched object handle. Thread safe promise settlement (resolve)\r\nis guaranteed, this promise implementation avoids exceptions not because of performance penalty but rather because\r\nthey are strings in AngelScript. This behaviour is controlled by user anyways and can be implemented fast.\r\n\r\nPromise class is a template for a reason, it needs a specific functor struct that will be called before context suspend\r\nand when context resume is requested. This allows one to implement promise execution in any manner: using thread pool, conditional variables, single threaded sequence of execute calls and using other techniques that could be required by their specific environment. This also allows informative debugging with watchers.\r\n\r\nImplementation does not have some features from other languages like JavaScript, for example **Promise.all**, these could be added through script file. Also promise does not contain **\\\u003cthen\\\u003e** function that is used pretty often in JavaScript. That is because unlike JavaScript in AngelScript every context of execution is it self a coroutine so that is considered bloat by my self to add chaining.\r\n\r\nPromise execution is conditional meaning early settled promises will never suspend context which improves performance and reduces latency. Also promise implementation uses AngelScript's memory functions to ensure support for memory pools and other optimizations.\r\n\r\nThis implementation supports important feature in my opinion: __co_await__ keyword brought directly from C++20, it works just like __await__ keyword in JavaScript but anywhere. This feature is not (yet?) AngelScript compiler supported so it requires an extra step over source code of script before sending it to compiler. See following usage examples:\r\n```cpp\r\n    promise\u003c...\u003e@ future = ...;\r\n    co_await future; // future could be a function call\r\n    co_await  (    future    );\r\n    co_await (future);\r\n    co_await co_await future; // nested await\r\n    co_await future[0].run_job(); // if future is an array\r\n    auto@ response = (co_await future) + \"output\"; // if future has plus op\r\n    if ((co_await (future)).is_succcess);\r\n    while ((co_await future).is_pending);\r\n\r\n    co_await(future) // Not supported, space is required\r\n```\r\n\r\nAnd final feature is naming customization, modifying preprocessor definitions in __promise.hpp__ you could achieve desired naming conventions. By default C style is used (snake-case). \r\n\r\n## How it executes\r\nThis example has two implementations for promise resolution (controlled by __ExecutionPolicy__ global variable in **examples/promises.cpp**):\r\n* Settlement thread executes next:\r\n    1. Thread A has started the execution\r\n    2. Promise await is called\r\n    3. Thread A sleeps or does something unrelated\r\n    4. Thread B settles the promise\r\n    5. Thread B continues the execution\r\n* Node.js event loop executes next:\r\n    1. Thread A has started the execution\r\n    2. Promise await is called\r\n    3. Thread A sleeps or does something unrelated\r\n    4. Thread B settles the promise\r\n    5. Thread B pushes a callback into a callback queue\r\n    5. Thread A awakes or reaches it's event loop\r\n    6. Thread A pops latest callback from a callback queue\r\n    7. Thread A continues the execution\r\n\r\n## Building\r\nCMake is build-system for this project, use CMake generate feature, no additional setup is required.\r\n\r\n## License\r\nProject is licensed under the MIT license. Free for any type of use.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromanpunia%2Faspromise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fromanpunia%2Faspromise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromanpunia%2Faspromise/lists"}