What's wrong with this code? Why I can see only one output? What returns futureUserByName
function? onComplete doesn't work for it as well. It must be just simple Future, but it doesn't work.
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object FeaturesPromises extends App {
final case class User(name: String)
def fetchUser(name:String): User = {
Thread.sleep(1000)
User(name)
}
def futureUserByName(userName: String): Future[User] = Future {
Thread.sleep(1000)
fetchUser(userName)
}
Future(fetchUser("John")).foreach(println)
futureUserByName("Jim").foreach(println)
Thread.sleep(5000)
}
Output>> User(John)
Output by sbt:
User(John)
[success] Total time: 10 s, completed Jul 5, 2024, 11:28:50 PM
[info] 7. Monitoring source files for root/run...
[info] Press <enter> to interrupt or '?' for more options.
User(Jim)
can anyone explain?
App
for a propermain
sinceApp
has many issues, so maybe is because of that.