0

I have a strange problem. I created two methods, which return Lists:

override def createContributorsList(url: String, params: String): F[List[Contributor]] = getContributorsFromClient(url, params).fold[List[Contributor]](_ => List(), res => res)

override def createReposList(organization: String, params: String): F[List[GitRepository]] = getReposFromClient(organization, params).fold[List[GitRepository]](_ => List(), res => res)

Now I want to call these methods (they connect to a Git repository and get contributors for a given organization):

val res = for {
    repos <- stats.createReposList("github", "")
    contr = repos.foreach(r => println(stats.createContributorsList(r.contributors_url,"").unsafeRunSync()))
  } yield (repos,contr)
 res.unsafeRunSync

As you see, I needed to call unsafeRunSync method twice to retrieve all the data. Why do I need to do it this way? Could it be done in another way? I think I should run unsafeRunSync only once - when I need to run program, not all the time.

Developus
  • 1,400
  • 2
  • 14
  • 50
  • what is the problem when you don't call it the first time? why do you have = instead of <- i n your for-comprehension – Saskia Nov 10 '19 at 21:56
  • When I have `<-` I got error that it cannot resolve this symbol. What do you mean by "call it first time"? – Developus Nov 10 '19 at 21:58
  • I mean when you call unsafeRunSync in the for comprehension. – Saskia Nov 10 '19 at 22:08
  • The problem is when you call unsafeRunSync the type of your expression is not IO anymore but Unit. And unit obviously has no flatmap/map. – Saskia Nov 10 '19 at 22:10

0 Answers0