I am writing a simple Silverlight application and WCF Service.
I want to create a synchronous method that return a value.
The method itself, call an asynchronous method from WCF Services. After I call asynchronous method, I want to get it value, and return to sender.
I hear that Rx can solve this kind of problem.

This is my code :

    private void btnCreate_Click(object sender, RoutedEventArgs e)
    {
        string myResult = getMyBook(txtBookName.Text);
        MessageBox.Show("Result\n" + myResult);
        // myResult will be use for another purpose here..
    }

    // I want this method can be called anywhere, as long as the caller still in the same namespace.
    public string getMyBook(string bookName)
    {
        Servo.ServoClient svc = new ServoClient();
        string returnValue = "";

        var o = Observable.FromEventPattern&lt;GetBookCompletedEventArgs&gt;(svc, "GetBookCompleted");
        o.Subscribe(
            b =&gt; returnValue = b.EventArgs.Result
            );

        svc.GetBookAsync(bookName);
        return returnValue;
    }


When I click btnCreate, myResult variable still empty. Is that something wrong with my code? Or maybe I am just don't understand with Rx concept? I am new to Rx.

My goal is : I need to get the result (myResult variable) from asynchronous method, and then used in later code.


Question: Chaining deferreds
Answer: I'm not sure if I understand you correctly, because your second function LoadFromText is some sort of black box for me. I think, though, that your construct is too bloated for the things executed. Try do do it in more functions that do less. I've done a proposition:

function Load() {
    var myUrl = '//path.to/url';
    LoadFromUrl(myUrl)
        .then(LoadFromText, errorHandler)
        .then(successHandler, errorHandler);
}

function successHandler(returnedData) {
    console.log('Requests were successful: ' + returnedData);
}

function errorHandler(err) {
    console.log('An error occurded: ' + err);
}

function LoadFromURL(url) {
    return $.get(url);
}

function LoadFromText(text) {
    // THIS IS SOME SORT OF BLACK BOX FOR ME...
    // SEEMS TO RETURN A PROMISE, THOUGH

    var $deferred = $.Deferred();

    // DO THE LOGIC TO EITHER REJECT OR RESOLVE THE PROMISE

    return $deferred.promise();
}


You only use the main function Load to start the ajax chain. As soon as the first ajax request is done, the second starts with the data of the first one. After that, the successHandler will do whatever is needed with the data of the second ajax call. This is the important addition to your solution.
Apart from that it's basically the same thing you did, but distributed to smaller functions that are easier maintainable.