How to use futures with many operations/computations

I’m stuck. I think I am missing something fundamental in my understanding of route calls and futures. What I am trying to achieve is something somewhat complicated, but It would be an easy task if not using futures. I would like to call functions not using requests from a specific route…

  1. I am letting users upload a file (like CSV formated) to server
  2. Interpreting/parsing the file and setting a lot of classes/models with data from that parsing and saving to database
  3. Extract information from the database and compare to other preset database-objects
  4. When finished I am serving the client with a new webpage filled with information from the calculations/operations.

This is not complicated using non-futures, but how should I go about to get it done with the complicated futures??

How can I call functions to get futures not using route?? Or… I missing something fundamental…

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi @sturm, the simple answer is to nest each asynchronous call. So before where you would have

let parsedFile = req.readCSV()
parsedFile.save()
let someModel = SomeModel.query().filter(\.name == "someName").first()
let newObject = NewObject(parsedFile.field, someModel.name)

You change it to:

return req.readCSV().flatMap { parsedFile in
  return parsedFile.save().flatMap { savedFile in
    return SomeModel.query().filter(\.name == "someName").first().flatMap { someModel in
      let newObject = NewObject(savedFile.field, someModel.name)
  }
}

Does that help to explain it?