# Executing blocking code in async/await

**URL:** <https://forums.swift.org/t/executing-blocking-code-in-async-await/71129>\
**Category:** Using Swift\
**Tags:** asyncawait, thread\
**Created:** [April 7, 2024, 7:50pm UTC](https://forums.swift.org/t/executing-blocking-code-in-async-await/71129 "2024-04-07T19:50:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cierpliwy](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cierpliwy/32/15271_2.png) [@Cierpliwy](https://forums.swift.org/u/Cierpliwy)\
**Post date:** [April 7, 2024, 7:50pm UTC](https://forums.swift.org/t/executing-blocking-code-in-async-await/71129/1 "2024-04-07T19:50:01Z")

</div>

Hello guys,

Do you know what's the most idiomatic way to offload blocking code to the separate thread or thread pool and get asynchronous function back? I'm looking at equivalent of [spawn\_blocking](https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html#method.spawn_blocking) from Rust's tokio create. It's important that this method should work also for Linux and Windows.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 7, 2024, 8:02pm UTC](https://forums.swift.org/t/executing-blocking-code-in-async-await/71129/2 "2024-04-07T20:02:29Z")

</div>

Something like this:

```swift
func spawnBlocking<T>(_ execute: @escaping () -> T) async -> T {
    await withCheckedContinuation { continuation in
        DispatchQueue.global().async {
            continuation.resume(returning: execute())
        }
    }
}

```

and if the function to execute is throwing:

```swift
func spawnBlocking<T>(_ execute: @escaping () throws -> T) async throws -> T {
    try await withCheckedThrowingContinuation { continuation in
        DispatchQueue.global().async {
            continuation.resume(with: Result(catching: execute))
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Cierpliwy](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cierpliwy/32/15271_2.png) [@Cierpliwy](https://forums.swift.org/u/Cierpliwy)\
**Post date:** [April 7, 2024, 9:27pm UTC](https://forums.swift.org/t/executing-blocking-code-in-async-await/71129/3 "2024-04-07T21:27:43Z")

</div>

Thank you for the response!

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [April 7, 2024, 11:36pm UTC](https://forums.swift.org/t/executing-blocking-code-in-async-await/71129/4 "2024-04-07T23:36:55Z")

</div>

> [@tera](#):
>
> ```swift
> func spawnBlocking<T>(_ execute: @escaping () -> T) async -> T {
> await withCheckedContinuation { continuation in
> DispatchQueue.global().async {
> continuation.resume(returning: execute())
> }
> }
> }
> 
> ```

Thank you, @tera

But, why does the following code return `(Function)`, not the expected numeric result of the computation?

```swift
@main
import Foundation

@main
enum AsyncAwaitBlockingCode {
    static func main () async throws {
        async let u = spawnBlocking {
            {() in 17 + 19}
        }
        
        await print ("-->", u, u ())
        
        async let v = spawnBlocking_throws {
            {() in 23 + 29}
        }
        
        try await print ("-->", v, v ())
    }
}

func spawnBlocking<T>(_ execute: @escaping () -> T) async -> T {
    await withCheckedContinuation { continuation in
        DispatchQueue.global().async {
            continuation.resume(returning: execute())
        }
    }
}

func spawnBlocking_throws <T>(_ execute: @escaping () throws -> T) async throws -> T {
    try await withCheckedThrowingContinuation { continuation in
        DispatchQueue.global().async {
            continuation.resume(with: Result(catching: execute))
        }
    }
}

```

```swift
--> (Function) 36
--> (Function) 52

```

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 7, 2024, 11:47pm UTC](https://forums.swift.org/t/executing-blocking-code-in-async-await/71129/5 "2024-04-07T23:47:58Z")

</div>

You have a nested closure, is that on purpose? If I got the intention right you want this:

```swift
        async let u = spawnBlocking { 17 + 19 }
        await print ("-->", u)
        async let v = spawnBlockingThrows { 23 + 29 }
        try await print ("-->", v)

```

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [April 8, 2024, 2:45am UTC](https://forums.swift.org/t/executing-blocking-code-in-async-await/71129/6 "2024-04-08T02:45:52Z")

</div>

> [@tera](#):
>
> You have a nested closure, is that on purpose?

Oops. 🫢

Thank you, @tera
