Skip to content

Instantly share code, notes, and snippets.

@NeuralGlue
Last active November 27, 2019 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeuralGlue/5958c1815239ecc323f2ef109b948f0e to your computer and use it in GitHub Desktop.
Save NeuralGlue/5958c1815239ecc323f2ef109b948f0e to your computer and use it in GitHub Desktop.
AntiPatterns in Vapor 3
// assuming a route such as /parents/Parent.parameter/children/
func addChildtoParent(_ req: Request) throws -> Future<Child> {
return try req.parameters.next(Parent.self).flatMap(to: Child.self) { parent in
return try req.content.decode(Child.self).flatMap{ child in
child.parentId = try parent.requireId()
return try child.save(on:req)
}
}
}
@NeuralGlue
Copy link
Author

This is an anti-pattern. Use instead Async's own flatmap(to: Result.type, FutureA, FutureB).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment