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

@:await JS Promises #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,39 @@ An @async function's return type will also be transformed. The following functio
}
```

## JS Promises

When `@await` is used on an expression that is a JS Promise, `tink_await` will extract the `T` from `js.lib.Promise<T>` (if it is a `js.lib.Promise`) and wrap the expression in a check-type statement:
`($expr : tink.core.Promise<T>)`.

This will leverage the magic of `tink_core` to silently convert the JS promise into a tink promise.

```haxe
var secret = @:await new SecretManagerServiceClient({
credentials: {
client_email: 'test',
private_key: 'test'
}
}).accessSecretVersion({
name: 'test'
});
```
Becomes:
```haxe
var secret = @:await (new SecretManagerServiceClient({
credentials: {
client_email: 'test',
private_key: 'test'
}
}) : tink.core.Promise<ts.Tuple3<google_cloud.secret_manager.build.protos.protos.google.cloud.secretmanager.v1.IAccessSecretVersionResponse, Null<google_cloud.secret_manager.build.protos.protos.google.cloud.secretmanager.v1.IAccessSecretVersionRequest>, Null<{}>>>
).accessSecretVersion({
name: 'test'
});
```

Try typing that 10 times quickly.


## Flags

- `-D await_catch_none`: Unexpected exceptions are never caught.
Expand Down
2 changes: 1 addition & 1 deletion src/tink/await/AsyncField.hx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class AsyncField {
var tmp = tmpVar();
return function() return process(em, ctx, function(transformed)
return function() return macro @:pos(em.pos)
$transformed.handle(${handler(tmp, ctx, next)})
tink.core.Promise.lift($transformed).handle(${handler(tmp, ctx, next)})
);
case EFor(it, expr):
if (!hasAwait(expr)) {
Expand Down