Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shiranuit/luacoro
Lua library made to do Async / Await using coroutines.
https://github.com/shiranuit/luacoro
async hacktoberfest lua promise
Last synced: 25 days ago
JSON representation
Lua library made to do Async / Await using coroutines.
- Host: GitHub
- URL: https://github.com/shiranuit/luacoro
- Owner: Shiranuit
- License: mit
- Created: 2021-08-06T08:12:01.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-10T15:22:13.000Z (over 1 year ago)
- Last Synced: 2024-10-16T10:41:42.181Z (2 months ago)
- Topics: async, hacktoberfest, lua, promise
- Language: Lua
- Homepage:
- Size: 41 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LuaCoro
Lua library made to do Async / Await using coroutines.## Functions
## Coro
### run
This will run the coro loop that will do all the async work.
Modes:
- **nil**: Defaulted to the "default" mode
- **default**: Block until there is no more async promises to be resolved
- **nowait**: Do one run of the async loop and return true if there is still more work to doReturn:
- **true**: if there is still some work to do
- **false**: if every promise has been resolved**Signature**
```lua
run(mode: or ):
```**example**
```lua
local coro = require('coro')local asyncAdd = coro.async(function(A, B)
return A + B
end)local main = coro.async(function()
print(coro.await(asyncAdd(2, 2)))
end)main()
coro.run()
```### async
Transform a function into an async function that returns a promise when called
**signature**
```lua
async():
```**example**
```lua
local asyncAdd = coro.async(function(A, B)
return A + B
end)
```### await
Await the result of a promise and returns what the promised was resolved to
**signature**
```lua
await( or )
```### range
Allows you to make ranged for loop that will not block the coro execution loop
**signature**
```lua
range(start: , stop: , [step: ])
```**example**
```lua
for i in coro.range(1, 10) do
print(i)
end
```### onUnhandledRejection
Register a callback that is going to be called when an exception occurs in a promise without being catched
**signature**
```lua
onUnhandledRejection(function(error: string, promise: ))
```**example**
```lua
local coro = require('coro')coro.onUnhandledRejection(function(err, promise)
print(err, promise)
end)
```### promise.new
Coro allows you to create promises that can be resolved later
```lua
local coro = require('coro')
local Promise = coro.promiselocal myPromise = Promise.new(function(resolve, reject)
--work
resolve(42)
end)coro.run()
```#### chain
Chains a function that will be executed once the previous promise is resolved.
the function will be given the result of what the promise has been resolved to.**signature**
```lua
:chain()
```#### catch
Chain a catch promise that will be executed if an error occurs in the promise
**signature**
```lua
:catch()
```#### await
Await a pending promise and returns what the promise was resolved to
**signature**
```lua
:await():
```#### resolve
Resolves the promise with then given value
**signature**
```lua
:resolve(...)
```#### error
Resolves the promise with an error
**signature**
```lua
:error(err)
```#### status
Returns the status of the promise, either "pending", "resolved" or "errored"
**signature**
```lua
:status(): 'pending' | 'resolved' | 'errored'
```#### promise.isErrored
Returns true if the promise is errored
**signature**
```lua
promise:isErrored():
``````lua
promise.isErrored():
```
#### promise.isResolvedReturns true if the promise is resolved
**signature**
```lua
:isResolved():
```
```lua
promise.isResolved():
```#### promise.isPending
Returns true if the promise is pending to be resolved or errored
**signature**
```lua
:isPending():
``````lua
promise.isPending():
```### promise.all
Takes a sequential table of promises and returns a new promise that will
be resolved when all the promises are resolved.The promise will be resolved with a table containing the result of all the resolved promises.
**signature**
```lua
all():
```### promise.waitForAll
Same as [all](#all) but await directly
**signature**
```lua
promise.waitForAll():
```### promise.any
Takes a sequential table of promises and returns a new promise that will
be resolved when one of the promises is resolved.The promise will be resolved with the result of the first resolved promise.
**signature**
```lua
promise.any():
```### promise.waitForAny
Same as [any](#any) but await directly
**signature**
```lua
promise.waitForAny():
```