Coder Social home page Coder Social logo

Comments (4)

cypressious avatar cypressious commented on September 23, 2024 1

Yes, runBlocking blocks, as the name suggests. Alternatively, use launch to start a coroutine without waiting for it. In any way, you can't just call suspend functions from non-suspend functions because they are compiled completely different. That's just how coroutines work.

from kotlinx.coroutines.

cypressious avatar cypressious commented on September 23, 2024

While this issue probably for Youtrack, I'll answer here:

You can always do runBlocking<Unit> {} in your non-suspend functions to cross the boundry to the coroutines world.

from kotlinx.coroutines.

ufoscout avatar ufoscout commented on September 23, 2024

@cypressious the workaround you propose is not the same, in fact, if I am not wrong, runBlocking<Unit> {} blocks the calling thread.
In addition, it uses a different context than of the one of the caller and it should be used only as top-level coroutine based on the documentation:

runBlocking { ... } works as an adaptor that is used here to start the top-level main coroutine. The regular code outside of runBlocking blocks, until the coroutine inside runBlocking is active.

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024

It is not just the different compilation. Suspending and non-suspending functions are conceptually different. suspend function can suspend the execution, while regular functions cannot do it conceptually. Think about it as a part of a function's type. For example, if your function is declared to return a String it cannot just go ahead and return an integer 32 instead. Similarly, if your function is declared with a guarantee that it never suspends (a regular function), it cannot just decide go ahead and decide suspend. That is so by design.

from kotlinx.coroutines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.