Translating a dataTask to a Promise/Future

Am 3 days new to Vapor, so pardon what may be a beginner question.

I am bringing a Swift command line app to Vapor, for which part of the work it does is repeating.

In the boot function, I call a method which in turn calls app.eventLoop.scheduleRepeatedTask(initialDelay:delay:)

In the method passed to scheduleRepeatedTask I execute a
session.dataTask(with: request) { (data, response, error) in...

I get the result back in the closure and can successfully parse it via Codable, but when I then try to process the parsed object I get a SIGABRT due to simultaneous access.

I imagine I need to turn the session.dataTask into a promise/futre instead, but I'm not finding any examples how to do that. Would appreciate any links. I've read through the docs.vapor.codes section on async, but still not clear from a high level how to approach the problem.

TIA.

What is the simultaneous access of? It seems that you're probably closing over a structure in your callback and attempting to mutate it in multiple places, and without knowing what your code is it's hard to suggest a restructuring that will make it work.

It looks like you're trying to use URLSession, I would suggest using Vapor's Client instead.

That might look something like this:

app.eventLoop.scheduleRepeatedTask(initialDelay: .seconds(15), delay: .seconds(15)) { (task) -> EventLoopFuture<Void> in
    let client = try app.make(Client.self)

    return client
        .get("http://someurl/mymodel")
        .map { (response) in
            let myModel = try response.content.syncDecode(MyModel.self)
            // do things with decoded model
        }
        .transform(to: ()) // Transform to void to satisfy the return type of EventLoopFuture<Void>
}