The goal of promise-async is to port Promise & Async from JavaScript to Lua.
A value returned by async function in JavaScript is actually a Promise Object. It's incomplete and inflexible for using an async function wrapped by bare coroutine without Promise in almost Lua implementation.
- API is similar to JavaScript's
- Customize EventLoop in any platforms
- Support Lua 5.1-5.4 and LuaJIT with an EventLoop module
- Support Neovim platform
promise-async-demo.mp4
promise-async/examples/demo.lua
Lines 17 to 82 in 3f6dcb2
promise-async/examples/demo.js
Lines 1 to 58 in 3f6dcb2
- Lua 5.1 or latter
- Luv
Luv is a default EventLoop for promise-async. It doesn't mean promise-async must require it. In fact, promise-async require a general EventLoop module which Luv like.
Install with Packer.nvim:
- As a normal plugin
use {'kevinhwang91/promise-async'}
or
- As a Luarocks plugin
use_rocks {'promise-async'}
luarocks install promise-async
luarocks install luv
or implement an EventLoop interface to adapt your platform
promise-async's API is based on MDN-Promise. typings/promise.lua is the typings with documentation of Promise class.
Summary up the API different from JavaScript.
JavaScript | Lua |
---|---|
new Promise |
Promise:new /Promise |
Promise.then |
Promise:thenCall , then is language keyword |
Promise.catch |
Promise:catch |
Promise.finally |
Promise:finally |
Promise.resolve |
Promise.resolve |
Promise.reject |
Promise.reject |
Promise.all : Symbol.iterator as iterator |
Promise.all : pairs as iterator |
Promise.allSettled : Symbol.iterator as iterator |
Promise.allSettled : pairs as iterator |
Promise.any : Symbol.iterator as iterator |
Promise.any : pairs as iterator |
Promise.race : Symbol.iterator as iterator |
Promise.race : pairs as iterator |
async : as keyword at the start of a function |
Async /Async.sync : as a surrounding function |
await : as keyword |
await /Async.wait as a function |
The environment in Async.sync
function have been injected some new functions for compatibility or
enhancement:
await
: A reference ofAsync.wait
function;pcall
: Be compatible with LuaJIT;xpcall
: Be compatible with LuaJIT;
async
in JavaScript return Promise object only with single result, but may carry multiple results
in Lua. The resolved result of Promise object return by async
function will be packed into a table
via {...}
. However, the result handled by await
will be unpacked and return multiple values.
local async = require('async')
local function f()
return 1, 2, 3
end
-- multiple results are packed into resolved result in Promise
async(f):thenCall(function(v)
print(v[1], v[2], v[3]) -- output: 1 2 3
end)
-- results returned by `await`
async(function()
local v1, v2, v3 = await(async(f))
print(v1, v2, v3) -- output: 1 2 3
end)
uv.run()
Promise.resolve():thenCall(cb)
is almost equivalent tovim.schedule(cb)
.
make test
Following typings/README.md
TODO, refer to loop.lua
- If you get an issue or come up with an awesome idea, don't hesitate to open an issue in github.
- If you think this plugin is useful or cool, consider rewarding it a star.
The project is licensed under a BSD-3-clause license. See LICENSE file for details.