Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active April 4, 2018 06:40
Show Gist options
  • Select an option

  • Save adamw/fe51387ca059c6124ebae38b1815b8f2 to your computer and use it in GitHub Desktop.

Select an option

Save adamw/fe51387ca059c6124ebae38b1815b8f2 to your computer and use it in GitHub Desktop.
class Callbacks {
// I/O operations: non-blocking, asynchronous
<T> void fetchFromDb(Class<T> entityClass, long id, Consumer<T> callback) { }
void sendHttpGet(String url, Consumer<String> callback) { }
void sendEmail(String to, String content, Runnable callback) { }
// the bussines logic: asynchronous
void runBusinessProcess(Runnable callback) {
fetchFromDb(User.class, 42,
// continuation: what should happen after the user is read from the db
user -> sendHttpGet("http://profile_service/get/" + user.getProfileId(),
profile -> sendEmail(user.getEmail(),
"Your profile is: " + profile,
callback)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment