aichaos / rivescript-js

A RiveScript interpreter for JavaScript. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
377 stars 144 forks source link

Parse inline JS object macro as async function #278

Closed kirsle closed 6 years ago

kirsle commented 6 years ago

This modifies the RiveScript parser for inline JavaScript macros to load them as async functions, which will allow them to use the await keyword to simplify dealing with Promises.

If your Node environment doesn't support async functions, it will parse them as normal functions as before (you won't be able to use the await keyword). For inline JS macros, your local JavaScript environment has to be modern enough to parse an async function (there's no Babel or anything at work here -- if you want async/await and want to be backwards compatible, define your macros via setSubroutine and use Babel to transpile your code).

This doesn't affect the setSubroutine() method: if you want an async function in setSubroutine, just use one:

bot.setSubroutine("test-macro", async function(rs, args) {
    return await rs.getUservar(rs.currentUser(), "name");
});

Fixes #275