Skip to content

Implements C# await/async keywords and behaviour with any JVM language

Notifications You must be signed in to change notification settings

soywiz-archive/jawaitasync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jawaitasync

Build Status

Implements C# await/async keywords and behaviour with any JVM language. Completely asynchronous.

import static jawaitasync.Promise.await;
import static jawaitasync.Promise.complete;
import static jawaitasync.PromiseTools.downloadUrlAsync;
import static jawaitasync.PromiseTools.sleepAsync;

public class DownloadUrlExample {
	public Promise downloadFilesAsync() throws IOException {
		String file = await(downloadUrlAsync("http://google.es/"));
		System.out.println(file);
		await(sleepAsync(1000));
		String file2 = await(downloadUrlAsync("http://www.google.es/"));
		System.out.println(file2);
		return complete(null);
	}
}

It supports try + catch handling:

package samples;

import jawaitasync.Promise;
import jawaitasync.PromiseTools;

import static jawaitasync.Promise.await;
import static jawaitasync.Promise.complete;

public class TryCatchExample {
	public Promise testAsync() {
		try {
			System.out.print("Started");
			await(PromiseTools.sleepAndThrowAsync(1000, new Exception("AfterASecondException")));
			System.out.print("NotRun");
		} catch (Exception exception) {
			System.out.print("MyCatch:" + exception.getMessage());
		}
		return complete(null);
	}
}

Examples: https://github.com/soywiz/jawaitasync/tree/master/src/main/java/samples

How does this works?

This behaviour is implemented post-processing class files. The process analyzes methods in class files trying to find Promise.await calls.

When it found a method calling await, it start reconstructing that method. Converts the method into a new class containing all the local variables as fields, and converts the function into a machine state stored as a method in that class.

And then rewrites the original method so it calls new classes.

Each await call (that accept promises), suspends the function, and when the promise is done, the function is resumed.

All this work is completely transparent. You just write the above code and it works.

In order to get it working you have to:

  • Run the class postprocessor jawaitasync (usually automatically), and run the code.
  • Use a provided custom ClassLoader, so the modifications are made on the fly at runtime without any postprocessing just when required.

About

Implements C# await/async keywords and behaviour with any JVM language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages