Jump to content

Lua Promise


ChrisMAN
 Share

Recommended Posts

Last week I wrote an emitter for doing easy events on objects. This week I wrote a library for handling deferred values. This is really nice for doing asynchronous programming. These 2 components are all that I really need to do async/evented programming.

 

https://github.com/friesencr/lua_promise

 

Promises are one of those things that are so elemental / simple they can be hard to grasp.

 

Here is a small example of what I might use it for.

 

function UsePotion()
 SelectPotion()
   :done(function(potion)
  NomNom(potion)
   end)
end
function SelectPotion()
 local p = Promise:new()
   gui = LoadPotionGui()
   gui:on('select_potion', function()
  p:resolve(gui.selected_potion)
   end)
 return p
end

 

 

Peace,

Chris

  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...