Coder Social home page Coder Social logo

baggins's Introduction

Hi there ๐Ÿ‘‹

I'm Alejandro Martinez, a.k.a @alexito4. I'm a Software Developer currently based in the beautiful city of Girona in Catalonia, Spain.

I love to share my knowledge and so I spent a bunch of time writing educational content, mostly about Swift and iOS development, on my website. I also create video content for my Youtube channel.

baggins's People

Contributors

alexito4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

baggins's Issues

Cancellation may not work as expected

Hi,

I've used your code for some time now and it's really useful and has helped me a lot. While refactoring some code to better handle user cancellation of long running tasks I noticed that I get a TimeoutError instead of a CancellationError sometimes. I was able to write a test case that shows it:

func testTimeout() async throws {
    let task = Task {
        do {
            try await withTimeout(2.0) {
                try? await Task.sleep(seconds:3.0)

                // Simulate needing to do a little work before politely exiting
                let delay = Task.detached {
                    try? await Task.sleep(seconds:0.5)
                }
                await delay.value

                try Task.checkCancellation()
            }
        } catch {
            throw error
        }
    }

    Task {
        try await Task.sleep(seconds: 1.0)
        task.cancel()
    }

    do {
        try await task.value
        XCTAssert(false)
    } catch {
        XCTAssert(error is CancellationError)
    }
}

Does it make sense, or is my test code not written correctly? I can solve the issue by changing the withTimeout function to:

public func withTimeout<R>(
    _ seconds: Double,
    _ work: @escaping () async throws -> R
) async throws -> R {
    try await firstOf {
        try await work()
    } or: {
        try await Task.sleep(seconds: seconds)
        throw TimeoutError()
    }
}

So basically, if the task that is running the withTimeout is cancelled, the or:-block will throw a CancellationError and the surrounding code can know the difference between a timeout and an active cancellation.

Try this, `withTimeout ` seems not work

`
func testAsync() async -> String {
var value = 0
for _ in 0...10_000_0000 {
value += 1
}
return "(value)"
}

    Task {
        do {
            let res = try await withTimeout(0.25) {

                await testAsync()
            }
            print(res)
        } catch {
            print(error)
        }
    }
    
  `

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.