Code Monkey home page Code Monkey logo

lua-async-await's Introduction

Async Await in 90 lines of code.

Originally written for Neovim, because it uses the same libuv eventloop from NodeJS.

Works for any LUA code

Special Thanks

svermeulen for fixing inability to return functions.

Preface

This tutorial assumes that you are familiar with the concept of async await

You will also need to read through the first 500 words of how coroutines work in lua.

Neovim use libuv for async, the same monster that is the heart of NodeJS.

The libuv bindings are exposed through luv for lua, this is accessed using vim.loop.

Most of the luv APIs are similar to that of NodeJS, ie in the form of

API :: (param1, param2, callback)

Our goal is avoid the dreaded calback hell.

Preview

local a = require "async"

local do_thing = a.sync(function (val)
  local o = a.wait(async_func())
  return o + val
end)

local main = a.sync(function ()
  local thing = a.wait(do_thing()) -- composable!

  local x = a.wait(async_func())
  local y, z = a.wait_all{async_func(), async_func()}
end)

main()

If you don't know how coroutines work, go read the section on generators on MDN.

It is in js, but the idea is identical, and the examples are much better.


Here is an example of coroutines in Lua:

Note that in Lua code coroutine is not a coroutine, it is an namespace.

To avoid confusion, I will follow the convention used in the Lua book, and use thread to denote coroutines in code.

local co = coroutine

local thread = co.create(function ()
  local x, y, z = co.yield(something)
  return 12
end)

local cont, ret = co.resume(thread, x, y, z)

Notice the similarities with async await

In both async await and coroutines, the LHS of the assignment statements receives values from the RHS.

This is how it works in all synchronous assignments. Except, we can defer the transfer of the values from RHS.

The idea is that we will make RHS send values to LHS, when RHS is ready.

Synchronous Coroutines

To warm up, we will do a synchronous version first, where the RHS is always ready.

Here is how you send values to a coroutine:

co.resume(thread, x, y, z)

The idea is that we will repeat this until the coroutine has been "unrolled"

local pong = function (thread)
  local nxt = nil
  nxt = function (cont, ...)
    if not cont
      then return ...
      else return nxt(co.resume(thread, ...))
    end
  end
  return nxt(co.resume(thread))
end

if we give pong some coroutine, it will recursively run the coroutine until completion

local thread = co.create(function ()
  local x = co.yield(1)
  print(x)
  local y, z = co.yield(2, 3)
  print(y)
end)

pong(thread)

We can expect to see 1, 2 3 printed.

Once you understand how the synchronous pong works, we are super close!

But before we make the asynchronous version, we need to learn one more simple concept.

For our purposes a Thunk is function whose purpose is to invoke a callback.

i.e. It adds a transformation of (arg, callback) -> void to arg -> (callback -> void) -> void

local read_fs = function (file)
  local thunk = function (callback)
    fs.read(file, callback)
  end
  return thunk
end

This too, is a process that can be automated:

local wrap = function (func)
  local factory = function (...)
    local params = {...}
    local thunk = function (step)
      table.insert(params, step)
      return func(unpack(params))
    end
    return thunk
  end
  return factory
end

local thunk = wrap(fs.read)

So why do we need this?

Async Await

The answer is simple! We will use thunks for our RHS!


With that said, we will still need one more magic trick, and that is to make a step function.

The sole job of the step funciton is to take the place of the callback to all the thunks.

In essence, on every callback, we take 1 step forward in the coroutine.

local pong = function (func, callback)
  assert(type(func) == "function", "type error :: expected func")
  local thread = co.create(func)
  local step = nil
  step = function (...)
    local stat, ret = co.resume(thread, ...)
    assert(stat, ret)
    if co.status(thread) == "dead" then
      (callback or function () end)(ret)
    else
      assert(type(ret) == "function", "type error :: expected func")
      ret(step)
    end
  end
  step()
end

Notice that we also make pong call a callback once it is done.


We can see it in action here:

local echo = function (...)
  local args = {...}
  local thunk = function (step)
    step(unpack(args))
  end
  return thunk
end

local thread = co.create(function ()
  local x, y, z = co.yield(echo(1, 2, 3))
  print(x, y, z)
  local k, f, c = co.yield(echo(4, 5, 6))
  print(k, f, c)
end)

pong(thread)

We can expect this to print 1 2 3 and 4 5 6

Note, we are using a synchronous echo for illustration purposes. It doesn't matter when the callback is invoked. The whole mechanism is agnostic to timing.

You can think of async as the more generalized version of sync.

You can run an asynchronous version in the last section.

Await All

One more benefit of thunks, is that we can use them to inject arbitrary computation.

Such as joining together many thunks.

local join = function (thunks)
  local len = table.getn(thunks)
  local done = 0
  local acc = {}

  local thunk = function (step)
    if len == 0 then
      return step()
    end
    for i, tk in ipairs(thunks) do
      local callback = function (...)
        acc[i] = {...}
        done = done + 1
        if done == len then
          step(unpack(acc))
        end
      end
      tk(callback)
    end
  end
  return thunk
end

This way we can perform await_all on many thunks as if they are a single one.

More Sugar

All this explicit handling of coroutines are abit ugly. The good thing is that we can completely hide the implementation detail to the point where we don't even need to require the coroutine namespace!

Simply wrap the coroutine interface with some friendly helpers

local pong = function (func, callback)
  local thread = co.create(func)
  ...
end

local await = function (defer)
  return co.yield(defer)
end

local await_all = function (defer)
  return co.yield(join(defer))
end

Composable

At this point we are almost there, just one more step!

local sync = wrap(pong)

We wrap pong into a thunk factory, so that calling it is no different than yielding other thunks. This is how we can compose together our async await.

It's thunks all the way down.

Tips and Tricks

In Neovim, we have something called textlock, which prevents many APIs from being called unless you are in the main event loop.

This will prevent you from essentially modifying any Neovim states once you have invoked a vim.loop funciton, which run in a seperate loop.

Here is how you break back to the main loop:

local main_loop = function (f)
  vim.schedule(f)
end
a.sync(function ()
  -- do something in other loop
  a.wait(main_loop)
  -- you are back!
end)()

Plugin!

I have bundle up this tutorial as a vim plugin, you can install it the usual way.

Plug 'ms-jpq/lua-async-await', {'branch': 'neo'}

and then call the test functions like so:

:LuaAsyncExample

:LuaSyncExample

:LuaTextlockFail

:LuaTextLockSucc

lua-async-await's People

Contributors

ms-jpq avatar svermeulen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

lua-async-await's Issues

Async functions with arguments do not work (parameters are ignored)

Simple example:

local do_thing = a.sync(function (val)
  local o = 10
  return o + val
end)

print(do_thing(5))

This should print 15, but instead it results in an error at line 3: attempt to perform arithmetic on a nil value (local 'val').

It seems like the library didn't pass the value 5 to the function.

Should await yield same result each time ? Why not just use existing Promise library like this ?

If you come from JS world you expect await to await a promise.
Thus awaiting multiple times should yield the same result.
A mature promise library seems to be this. There are more on github:

https://eryn.io/roblox-lua-promise/docs/WhyUsePromises

const wrap = (f) => (...args) => new Promise((r,j) => f(args, (e,r) => { if (err) j(err); r(r) }))

And that wrapper already exists from changelog:

  • Promise.promisify now uses coroutine.wrap instead of `Promise.spawn

but that requires tasks and some game engine..

Here is yet another implementation:
https://github.com/javalikescript/luajls

Question: How to call an async function synchronously

Hello @ms-jpq , thanks again for the lovely code and tutorial. It is helping me slowly get my head around asynchronous programming. Can you tell me how you would call an one of your async function synchronously?

For example, using your tutorial code:

print('sync example start ')
print( async_tasks_2(1)())
print('sync example end')

prints

sync example start                                                                                                                                                                                                              
sync example end
1 2
3 4

I would like it to print

sync example start                                                                                                                                                                                                              
1 2
3 4
sync example end

:-)

Tutorials in other languages talk about blocking until the task is finished, but if I try to wait without the surrounding sync, there are issues.

Use tail calls where applicable

Lua specifies tail calls as part of the language, which can be taken advantage of in the code presented.

However, having function A calling function, B right at the end, like the following is not enough.

function B() dosomething() end

function A()
    B()
end

This is because lua doesn't know how many items function B will return and has to clean the lua stack up in case function B returns anything, because A is not supposed to return anything itself. This means that the last thing function A does is clean up the lua stack after B returns, not calling function B itself. Because of this, the tail call optimization cannot be applied.

However in the following similar snippet:

function B() dosomething() end

function A()
    return B()
end

Here, since function A returns exactly the same thing as function B, the last thing function A does really is calling function B (because after B returns, the lua stack will contain exactly what A needs to immediately return). So in this case, the call to B will be tail-call optimized and no extra stack space is used.

An example of where this optimization can be used (because we don't really care what the relevant functions return) is in the step function within the pong function, for example:

step = function(...)
    local stat, ret = coroutine.resume(thread, ...)
    assert(stat, ret)
    if coroutine.status(thread) == 'dead' then
        return (callback or function() end)(ret)
    else
        assert(type(ret) == 'function', 'type error :: expected func')
        return ret(step)
    end
end

and in the callback inside the join function:

local callback = function(...)
    acc[i] = { ... }
    done = done + 1
    if done == len then
        return step(unpack(acc))
    end
end

License?

Is there a license for the code in this repo? Was following along as I have a need to do some async work and wait for the result.

Cannot return functions in async methods

Using the implementation here, you cannot create async functions that return functions, since the returned functions will always be interpreted as a 'thunk'. I attempted to modify the pong method to allow this. In case it's useful to others, I wanted to share what I'm using instead:

local pong = function (func, callback)
  assert(type(func) == "function", "Expected type func but found " .. type(func))
  local thread = co.create(func)
  local step = nil
  step = function (...)
    local go, ret = co.resume(thread, ...)
    assert(go)
    if co.status(thread) == "dead" then
      if callback then
        callback(ret)
      end
    else
      assert(type(ret) == "function")
      ret(step)
    end
  end
  step()
end

This allows function return values because it always calls the callback value when the coroutine is dead. Also, I think we can assume that the go value is always true now that we are checking the co.status immediately after the call to resume

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.