-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async-Await to support User Variable Session Managers #220
Comments
for those running rivescript server side, async await is working now in node7 |
Yay for porting to ES6/7 or even better, Typescript :) |
we've actually stopped using typescript a bit recently for new apps, now that node has async/await support... you can also get someway with typescript is still preferable, but it just adds time/overhead when bringing on new people. |
Updates: I looked into CoffeeScript's async/await support and wrote the following example script that works: #!/usr/bin/env coffee
resolveAfter2Seconds = (x) ->
return new Promise (resolve, reject) ->
setTimeout(->
resolve(x)
, 2000)
main = ->
console.log "program start"
res = await resolveAfter2Seconds("hello")
console.log "res1: %s", res
res2 = await resolveAfter2Seconds("bye")
console.log "res2: %s", res2
console.log "program end"
main() The following were required to get this script to work:
To resolve this ticket, the following sorts of changes are needed to RiveScript (thankfully these should be very small):
|
This is a placeholder ticket and a new place to discuss how to implement a user variable session manager interface (to allow botmakers to automatically persist user variables in Mongo or MySQL or so), like the Python, Go and Java versions have.
The upcoming EcmaScript Async Await may finally make it possible to support user variable session managers, which can support async calls to a database by "pausing" execution within RiveScript while it awaits a database lookup before continuing.
Out of curiosity just now I was looking at the documentation for node-mongodb-native (to see whether it supported synchronous actions) and saw they were using this co library in their example snippets, which allowed for writing generators/coroutines that can 'yield' to async functions and wait for them to resolve before continuing execution 'synchronously'; the linked MongoDB example uses it to serialize database operations without chaining promises together.
It seems that with Babel we may be able to use async/await now in a way that can be backported to support ES5 (by translating async functions into generators automatically). The code changes necessary to RiveScript would probably be small and not require the massive refactoring that I expected it would to support user session managers:
await
keyword.await
throughout the code when interacting with the session manager interfacesThe other thing I'll have to investigate is how well CoffeeScript supports async functions. This pull request came up after googling for a few minutes which seems to suggest that the CoffeeScript compiler will detect functions that call
await
and make themasync
implicitly, but I'll have to see how that works in reality. Otherwise, this may be a good time to drop CoffeeScript and port the code to ES2016+ so that we're not held back anymore by the limitations of third party transpilers.The text was updated successfully, but these errors were encountered: