Waiting for a coroutine to complete before getting a return value

Here is what I am trying to do:

I have a program that requires a player be logged into Facebook in order to perform certain functions. I have the Facebook login process which works by taking delegate as a parameter and then calling that delegate upon completion of the login with a bool value representing success or failure.

Here is where it gets a bit tricky. I want to fully encapsulate the login command within a class so that the user of the class does not have to worry about the login specifically when attempting to query data. For example, I have another function which retrieves the friends list of the user who is currently logged in. If the GetFriends function is called I would first like to check if a user is logged in and, if so, simply return the friends list which would have already been retrieved. However, if not logged in, this function will throw up the login prefab and wait for the user to log in. Once the user has done so it will then retrieve the friends list and return it back as the return value to the GetFriends function that initiated this process.

I have thought about having the user of the class check the login state first but this tends to get a bit messy with having to themselves create delegates and wait for the callback to ensure login occurred before being able to query friends. I would much prefer my class takes care of this.

I have attempted to do this with coroutines but have had difficulties returning the friends list back up to the original calling function. I have also had trouble getting a function whose type is not IEnumerator to wait for a coroutine to finish (in this case the coroutine is the login process). Can anyone suggest a good way to do what I’m asking? I really need the login to be handled by my class and not place this responsibility upon the user of my class.

To try to sum it up, I need this function:

  public List<FacebookFriend> GetFriends() 

to be able to login, if needed, and then return the friends list back.

I would try something like this:

using UnityEngine;
using System.Collections;

public class Facebook: MonoBehaviour {

    private List<FacebookFriend> friendList = null;
    private bool busy = false;

    public static List<FacebookFriend> GetFriends(){
        if (friendList == null){ // friend list not ready:
            // if CoroutGetFriends not running yet, start it:
            if (!busy) StartCoroutine(CoroutGetFriends());
        return friendList; // return null if not ready

    IEnumerator CoroutGetFriends(){
        busy = true; // CoroutGetFriends is running now
        if (!logged){ // not logged yet?
            // chain to Login coroutine:
            yield return StartCoroutine(Login());
        // user logged in by now: get friends
        busy = false; // CoroutGetFriends ended

    IEnumerator Login(){

In the calling script, call GetFriends to get the list: if it’s ready, GetFriends just returns it; if not, it returns null and starts the login process automatically - do nothing while GetFriends return null:

void Update(){
    List<FacebookFriend> friends = Facebook.GetFriends();
    if (friends != null){
        // the list is ready: do whatever you want to do with it