Skip to content
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 #1002

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Async/await #1002

wants to merge 2 commits into from

Conversation

TwitchBronBron
Copy link
Member

@TwitchBronBron TwitchBronBron commented Jan 9, 2024

Adds async/await support to brighterscript.

Leverages the @rokucommunity/promises library to support syncronous-looking flows into promise-drive async flows. For example:

async function getAuthToken()
    try
        username = await getUsernameAsync()
        password = await promptForPassword(username)
        authToken = await getAuthToken(username, password)
        return authToken
    catch e
        print e
    end try
end function

Would transpile into something like:

function getAuthToken()
    scope = {}
    return promises_chain(getUsernameAsync()).then(function(username)
        scope.username = username
        return promptForPassword(scope.username)
    end function).then(function(password)
        scope.password = password
        return getAuthToken(scope.username, scope.password)
    end function).catch(function(e)
        print e
    end function).toPromise()

There are many complex use cases that we won't be covering in this first iteration (like if statements, loops, etc). For now, we will handle the simple case of top-level function statements and try-catch.

Tasks:

  • 1. Add lexer and parser support for async and await. Both should be fully allowed as local variables, function names, etc. But when used in the context of an async function, or an await statement, it will result in new AST.

    • add a new token on FunctionStatement called asyncToken. This is how we will determine if the function is async or not.
    • Create a new AwaitedExpression that will wrap the value to the right.
    • for now, in validation we should flag any AwaitedExpression that are not at the root of a function body. In the future we will add more support for nested expressions, but that gets complicated quick...
  • 2. Integrate the @rokucommunity/promises library into brighterscript in a similar fashion to bslib. The promises library should only be included IF async/await is used. Also, if the project already has an instance of @rokucommunity/promises, then bsc shouldn't include another copy and should instead leverage that one.

  • 3. Write the transpiler logic for async/await.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants