Skip to content

Async await#1093

Merged
Perryvw merged 31 commits intomasterfrom
async-await
Aug 20, 2021
Merged

Async await#1093
Perryvw merged 31 commits intomasterfrom
async-await

Conversation

@Perryvw
Copy link
Member

@Perryvw Perryvw commented Aug 18, 2021

Implemented support for async/await using generators/yields as explained in this article. The actual implementation is based on the TypeScript polyfill as included in the comments in the lualib source.

Generators and yield are implemented in tstl using coroutines, for now it is not possible to use await at the very top level of your code because that would be outside of coroutines, for now await must appear in an async function.

Copy link
Collaborator

@tomblind tomblind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have tests of throwing errors inside async functions as well as catching them with with try...catch blocks.

Also, while it should work fine, a good test would be awaiting inside of a statement that gets wrapped in an IIFE, like a complex conditional :?.

Copy link
Collaborator

@tomblind tomblind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was about to approve this when I realized something. This isn't going to play with varargs at all. Currently it will optimize and try to directly use the ellipses. But even if you disable vararg optimzation for async functions, the wrapping of the ellipsis will still occur inside the function expression, which will break. The wrapping of the ellipsis needs to get lifted out somehow.

async function foo(...args: unknown[]) {
    console.log(...args);
}
function foo(self, ...)
    return __TS__AsyncAwaiter(
        function()
            local args = {...}
            print(
                table.unpack(args)
            )
        end
    )
end

Co-authored-by: Tom <tomblind@users.noreply.github.com>
@Perryvw Perryvw merged commit 98ffee6 into master Aug 20, 2021
@Perryvw Perryvw deleted the async-await branch August 20, 2021 13:07
@hubol
Copy link

hubol commented Aug 20, 2021

I'm so excited to use this!!! Thank you

sanikoyes pushed a commit to sanikoyes/TypeScriptToLua that referenced this pull request Sep 24, 2021
* Initial lualib promise class implementation

* First promise tests

* More promise tests

* Promise class implementation

* Implemented Promise.all

* Promise.any

* Promise.race

* Promise.allSettled

* fix prettier

* Add promise example usage test

* Added missing lualib dependencies for PromiseConstructor functions

* Immediately call then/catch/finally callbacks on promises that are already resolved

* Transform all references to Promise to __TS__Promise

* PR feedback

* Removed incorrect asyncs

* Add test for direct chaining

* Add test for finally and correct wrong behavior it caught

* Added test throwing in parallel and chained then onFulfilleds

* Fixed pull request link in ArrayIsArray lualib comment

* Initial async await

* Disallow await in top-level scope

* Add await rejection test

* Give await the correct lualib dependencies

* use coroutine.status instead of lastData

* Better top level await check

* Add tests for async lambdas and throws in async functions

* Moved toplevel await check to transformAwaitExpression and removed superfluous try/catch

* fix for vararg access in async functions (TypeScriptToLua#1096)

Co-authored-by: Tom <tomblind@users.noreply.github.com>

Co-authored-by: Tom <26638278+tomblind@users.noreply.github.com>
Co-authored-by: Tom <tomblind@users.noreply.github.com>
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.

4 participants