Code Monkey home page Code Monkey logo

lua-promise's Introduction

lua-promise

promise(A+) for lua. like ES6

run in esp8266 ui

Build Setup

# install dependencies
luarocks install lua-promise
  • In NodeMcu

1.Download lua-promise.lua

2.Open in ESPlorer and Save to ESP

3.Restart ESPXXXX

Api

  • For the Promsie class, call with '.'

Promise.new(executor)

promise = Promise.new(function(resolve, reject) .. end);

Promise.all(array)

promise = Promise.all(array)  -- a table as array

Promise.race(array)

promise = Promise.race(array) -- a table as array

Promise.reject(reason)

promise = Promise.reject(reason); -- reason is anything

Promise.resolve(value)

promise = Promise.resolve(value);
promise = Promise.resolve(promise);
  • For promise instance, call with ':'

promise:next(onResolve, onRejecte)

promise2 = promise:next(functoin(value) ... end);
promise2 = promise:next(nil, functoin(reason) ... end);

promise:catch(onRejecte)

promise2 = promise:catch(functoin(reason) ... end)

Usage

example

Promise = require('lua-promise')

login = Promise.resolve({err = 0, data = {id = '1001', name = 'admin'}}):next(function (ret)
    if(ret.err == 0) then
        -- login success
        return ret.data.id
    else
        error(ret)
    end
end)
getMenuConfig = function(id)
    return Promise.resolve({home = '/home/'..id..'.html'})
end
getNickName = function (id)
    return Promise.resolve('nickName'..id)
end

login:next(getMenuConfig):next(function (menu)
    print(menu.home)
end)
login:next(getNickName):next(function (nickName)
    print(nickName)
end)

test code

Promise = require('lua-promise')

Promise.new(function (resolve, reject)
    resolve('aaaa')
end):next(function (ret)
    print('1', ret)
    return Promise.resolve('bbbb'):next(function (ret0)
        print('1.1', ret0)
        return ret0..'cccc'
    end)
end):next(function (ret)
    print('2', ret)
    return Promise.reject({code = -1})
end):catch(function(err)
    print('err', err, err.code)
    return '000'
end):next(function (ret)
    print('3', ret)
    return 0 / nil
end):next(function (ret)
    print('4', ret)
end):catch(function(err)
    print('err2', err)
end)

Promise.all({Promise.resolve(1), Promise.resolve(2)}):next(function (rets)
    for i = 1, #rets do
        print('rets', rets[i])
    end
end)

Promise.race({Promise.resolve('a'), Promise.reject('b'), Promise.resolve('c')}):next(function (ret)
    print(ret)
end):catch(function(err)
    print('err', err)
end)

lua-promise's People

Contributors

hjmmc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.