I have two objects bookmark, bookmarks, i want to pass object as parameters to Scala Templates like
return ok(views.html.bookmarks.list.render(bookmark, bookmarks));
in F.Promise,
public static F.Promise<Result> get(final String id) {
F.Promise<Bookmark> bookmark = Bookmark.findById(id);
F.Promise<Collection<Bookmark>> bookmarks = Bookmark.findAll();
// here how to write the code that should pass both objects to scala template
}
How to write the return statement to pass these two objects in F.Promise without Json Result?
You need to map the result of the future, and if you want to pass 2 objects out of it you can use a Tuple. You can also improve this by making the database calls concurrently with zip. When you use
zip()
you'll improve the efficiency of the code and automatically get aTuple
out of it.This doesn't take into account error handling, i.e. a Bookmark with
id
not being present. For that, you'll need to look atrecover
orrecoverWith
.