1
\$\begingroup\$

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);
\$\endgroup\$

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

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

  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Amith George Aug 22 '12 at 13:23
  • \$\begingroup\$ I agree with @AmithGeorge, the code above doesnt look that messy to me. \$\endgroup\$ – sQVe Aug 22 '12 at 13:25
  • \$\begingroup\$ @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! :) \$\endgroup\$ – JonWells Aug 22 '12 at 13:28
  • 1
    \$\begingroup\$ @ChrimsonChin: You don't need to re-post, moderators can move questions - just flag it for mod attention \$\endgroup\$ – Bergi Aug 22 '12 at 13:31
2
\$\begingroup\$

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);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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