Code Monkey home page Code Monkey logo

gmodasync's Introduction

Garry's Mod Async

A basic async library for Garry's Mod. Written in MoonScript, must be compiled for use in the game.

Usage

Usage is fairly simple. This library uses JavaScript-like Promises, but you won't need to instantiate them directly.

All functions are stored in the global Async by default. You can ensure this global exists with require 'async'.

To use this module, you will need to compile lua/includes/modules/async.moon using moonc. You can find instructions on installing the Moonscript compiler here.

Alternately, you can download the Lua file from Releases and place it under lua/includes/modules.

Creating a Promise

This is easy! In either Lua or MoonScript, you can pass Async.async a function to create a promise easily. For example:

    import async from Async
    fn = async -> 
        print 'hey'
        return 'ho'
    
    fn()\andThen (data) ->
        print data

(or, in Lua, a bit less pretty)

    local async = Async.async
    
    local fn = async(function()
        print('hey')
    end)

    fn():andThen(function()
        print('ho')
    end)

These would both produce the following output:

hey
ho

Note that this is non-blocking, and will not run inline. This is by design, to finish in a thread before continuing with the promise.

The function passed will be run initially next tick, and if it returns data or errors, the promise will immediately resolve. Since the first function returns right away, the andThen will be called right away. Otherwise...

fulfill!

Within any asynchronous function, you have access to the fulfill() function without needing to declare it. You can use it to fulfill Promises in the future, running their andThens at an arbitrary later date. This is fairly standard if you've used promises before. I've included an example below:

    import async from Async
    fn = async -> 
        timer.Simple 1, fulfill
    
    fn()\andThen -> 
        print 'ho'
    
    print 'hey'

This will print hey, as expected, then, a second later when the function resolves, print ho. Any arguments passed to fulfill will be passed to the function registered under andThen if the initial Promise doesn't return.

Notable is that a Promise cannot resolve more than once. This means if you return something, then call fulfill later, the andThen chain will not be run again.

reject!

Similar to fulfill, except will run the onError chain. Not much more needs said, it functions identically to fulfill, except it only runs on initial error, or if called directly.

andThen

Discussed in fulfill, any functions in the andThen chain are called when a Promise returns a value initially, or when fulfill is called. Accepts a single function argument.

onError

Discussed in reject, called when this is called, or when the Promise initially errors out. Also accepts a single function argument.

butAlways

This runs the first time the Promise initially returns a value or errors out, or is fulfilled or rejected. Functions in this chain are not passed any arguments from either of these function, but always run after the Promise resolves.

Chaining

Not super necessary in MoonScript, since it has with, but included for Lua users. Any time you call andThen, onError, or butAlways, the functions will return their parent object, allowing chaining. For example, the following code works as expected in Lua:

    local async = Async.async
    local fn = async(function()
        return true
    end)
    fn:andThen(function() print(1) end)
      :andThen(function() print(2) end)
      :onError(function() print('Something went wrong') end)
      :butAlways(function() print('It's all over now') end)

And would output in the order registered, andThen or onError first, then butAlways. In this case:

1
2
It's all over now

gmodasync's People

Contributors

ssblur avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.