https://github.com/iamcco/async-await.lua
Write async function more like javascript async/await
https://github.com/iamcco/async-await.lua
Last synced: 3 months ago
JSON representation
Write async function more like javascript async/await
- Host: GitHub
- URL: https://github.com/iamcco/async-await.lua
- Owner: iamcco
- License: mit
- Created: 2021-04-26T03:42:44.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-25T06:32:47.000Z (about 3 years ago)
- Last Synced: 2025-03-18T20:54:04.470Z (10 months ago)
- Language: Lua
- Homepage:
- Size: 2.93 KB
- Stars: 35
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# async-await.lua
Write async function more like javascript async/await
#### `await` the async callback function
``` lua
local aw = require('async-await')
local timer = vim.loop.new_timer()
aw.async(function ()
local hello = aw.await(function (cb)
timer:start(1000, 0, function ()
cb('hello')
end)
end)
local world = aw.await(function (cb)
timer:start(1000, 0, function ()
cb('world')
end)
end)
print(hello, world)
end)
```
#### `await` the `async` function
``` lua
local aw = require('async-await')
local timer = vim.loop.new_timer()
local hello_world = aw.async(function ()
local hello = aw.await(function (cb)
timer:start(1000, 0, function ()
cb('hello')
end)
end)
local world = aw.await(function (cb)
timer:start(1000, 0, function ()
cb('world')
end)
end)
return hello,world
end)
aw.async(function()
local hello, world = aw.await(hello_world)
print(hello, world)
end)
```