Suspend Function In Kotlin-Coroutines
Tags: android, kotlin, coroutines
Published: 2021-06-30 11:39:54

Before Getting Started I Suggest You Understand What's Actually A Coroutine Is, Which I Have Discussed In The [Previous Blog](https://dev.to/saketh/getting-started-with-coroutines-in-kotlin-kek). And `Make Sure` That You Have **Included Coroutines Dependency** If It's Not Included:
```
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
```

**Suspend Function:** Well, Suspend Function Is A **Simple Function** Which **`Can Be Paused` And `Resumed Later In Your Code`**.


> 
Suspend Function Can Execute Long-Running Operations || Tasks And Will Wait For It To Complete Without Blocking The Whole Code.

And The **`Speciality` Of Suspend Function** Is That It **`Can Be Only Implemented In Another Suspend Function || Coroutine`**. 

`delay()` Is Also A Suspend Function That delays the coroutine for a specific time.

If You Are Trying To Implement `delay()` In Your Code, Your `IDE May Show This Image` In The Numerical Panel `Where You Implemented A Suspend Function`👇🏼

![Suspend Function Symbol.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625131906546/2LZvyLPHU.png)

> This Image Simply Means That You Are Implementing A Suspend Function.

As I Mentioned That `delay()` Is Also A `Suspend Sunction` Which Means That You Should Implement `delay()` In A `Suspend Function` **Or In A**  `Coroutine`

->But In Case If You Are Trying To **Implement Suspend Function `Outside Of A Supend Function Or A Coroutine`** Leads To `This Beautiful Error🤭`:
```
Suspend function 'delay' should be called only from a coroutine or another suspend function
``` 
->Error May Look Like This If You Are Trying To Implement Suspend Function Outside Of A Supend Function Or A Coroutine In Your Android Studio:


![DelayFunctionError.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625034385074/53y0mXdqE.png)
->Let's Build A Suspend Function In Android To Deep-dive:

->For Creating A Suspend Function We Need Mention `suspend` Before You Create The Function:
```
suspend fun delaying() {
        //Code
}
```
->Returning A String Value From The `delaying()` Function:
```
suspend fun delaying():String{
        return "Stay Safe😷"
}
```
->As I Mentioned That `Suspend Function Can Be Called || Implemented In Suspend Function Or In A Coroutine`.

->Let's **Launch A Coroutine** By Assigning `GlobalScope.launch{}` In Your Main Thread:
```
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        GlobalScope.launch {
            //Code
        }
    }
    suspend fun delaying():String{
        return "Stay Safe😷"
    }
```
`Note:` You `Should NOT` Use **GlobalScope** every time.

->Now Let's Implement `delaying()` In Coroutine:
```
GlobalScope.launch {
  delaying()
}
```
->Now If You Are Trying To Implement `delaying()` Outside, Of A Coroutine You'll Get This Error As `delaying()` Is A **Suspend Function**:

![suspend delaying() error.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625034256876/X0l91hBAs.png)
->Let's Assign `Log`To Check If The `delaying()` Is Working Or Not:

```
GlobalScope.launch {
            val checking=delaying()
            Log.d("suspend", "${checking} Is Running In ${Thread.currentThread().name}")
}

suspend fun delaying():String{
   delay(2000L)
   return "Stay Safe😷"
}
```
->Now Run The Application And Open Logcat To Check The Result:

![logcat message.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625034457374/Swsew5sFb.png)
Well That's All For Now, In **Upcoming Blog** I'll Discuss **Coroutine Contexts😉**

>**`Bye🤗`**