Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a little chain of JQuery promises. My problem is at each stage I want to notify my progress to the user, keep them in the loop (Perhaps using .notify somehow?)

What I've eneded up with is this bizarre mess of callbacks and promises. It works but I'm quite certain there is a neater, more logical way to do this.

both fetchSomeData() and Parser() return a promise

    var parseData = function(json)
    {
        showMessage("Updating your data...");
        return new Parser(json);
    };

    var onParseDone = function()
    {
        hideMessage();
        updateView();
    };

    showMessage("Fetching a bit of data...");
    fetchSomeData().pipe(parseData).done(onParseDone);
share|improve this question

migrated from stackoverflow.com Aug 22 '12 at 13:51

This question came from our site for professional and enthusiast programmers.

2  
Am curious, why do you feel the code you posted is a mess? As in, what sort of pseudo code would look pretty? Maybe once I can see where the mess is, I can attempt to clean it up :) If possible, update the question and post a comment stating the same. – Amith George Aug 22 '12 at 13:23
    
I agree with @AmithGeorge, the code above doesnt look that messy to me. – sQVe Aug 22 '12 at 13:25
    
@AmithGeorge it feels like its a bit of strange combination of promises and functions. Don't get me wrong, if general consensus is its good than I'm happy with that! :) – CrimsonChin Aug 22 '12 at 13:28
1  
@ChrimsonChin: You don't need to re-post, moderators can move questions - just flag it for mod attention – Bergi Aug 22 '12 at 13:31
up vote 2 down vote accepted

Surely this is possible. Only the fetch and parse functions would need to call notify with the respective message on the Deferreds they return. The code then could look like this:

fetchSomeData().pipe(parser).progress(showMessage).done(hideMessage, updateView);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.