I have this group of code, I only want to run: `self.performSegue` AFTER all of the for loops and all of the Firebase's asynchronous tasks have finished running:

```
    getFeaturedPost(withCompletion: startNext)

    func getFeaturedPost(withCompletion completion: () -> Void ) {
    print("Getting featured posts...")
    ref.child("featured").child("amount").observeSingleEvent(of: .value, with: { (snapshot) in
        self.numberOfPosts = snapshot.value as! Int

        print("There's \(self.numberOfPosts) posts avaliable.")

        for pos in 0..<self.numberOfPosts{
            print("Getting reference names for post: \(pos + 1)")
            self.ref.child("featured").child("post\(pos + 1)").observeSingleEvent(of: .value, with: { (snapshot) in
                let postID = (snapshot.value as? NSDictionary)?["postID"] as? String ?? ""
                let userOfPost = (snapshot.value as? NSDictionary)?["userOfPost"] as? String ?? ""
                self.customValues.append(("/users/public/\(userOfPost)/posts/\(postID)"))
            })
        }
    })
    print("Done! The posts are: \(customValues)")
    completion()
}

func startNext()
{
    getPostData(withCompletion: {
        print("Finished getting data, going to main screen.")
        self.performSegue(withIdentifier: "showHome", sender: nil)
    })
}

func getPostData(withCompletion completion: () -> Void ) {
    print("Getting idividual post data, there are \(customValues.count) posts")
    for i in 0..<customValues.count {
        print("Currently on post: \(i)")
        let encodedURL = (customValues[i] + "/postURL")
        ref.child(encodedURL).observe(.value, with: { (snapshot) in
            if let newURL = snapshot.value as? String{
                print("Sending \(newURL) to DemoSource Class")
                DemoSource.shared.add(urlString: newURL)
            }
        })
    }
    completion()
}

```

Yet the `startNext()` function (which goes to the next view) is executed before `getFeaturedPost`'s starts it's for loop where it prints what post it'c currently at. By the end when I send the data to the demosource class with `DemoSource.shared.add(urlString: newURL)` the `newURL` is nil, I have a console log which shows you the order of the print statements of each function:

```
Getting featured posts...
Done! The posts are: []
Getting idividual post data, there are 0 posts
Finished getting data, going to main screen. // This line should be printed last meaning this function is being executed too early
There's 2 posts avaliable.
Getting reference names for post: 1 // These two lines should be called before the line 'Finished getting data'
Getting reference names for post: 2

```