Kotlin / dukat

Converter of <any kind of declarations> to Kotlin external declarations
553 stars 44 forks source link

Generate suspending function when JS function returns Promise #361

Open vlsi opened 4 years ago

vlsi commented 4 years ago

Lots of JS functions return Promise, and it results in lots of .await() in the user-code, and it is prone to error, as it is very easy to miss .await() call.

AFAIK xxxAsync functions are discouraged in Kotlin. Adding explicit xxxAsync suffix to the imported from .d.ts Promise-returning functions makes it easier to spot that the result needs .await(), and it enables having a clean name for a suspending function.

For instance (@actions/io):

export declare function which(tool: string): Promise<string>;

The idea is to convert it as

@JsName("which")
external fun whichAsync(tool: String): Promise<String>

suspend fun which(tool: String) = whichAsync(tool, check).await()

I don't know if there's a generic way to handle definedExternally arguments.