Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-72609

Convert pendingPromise to native Promise

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Minor Minor
    • 4.2
    • 4.0, 4.2
    • JavaScript
    • MOODLE_400_STABLE, MOODLE_402_STABLE
    • MOODLE_402_STABLE
    • MDL-72609-master
    • Hide

      Behat all the way (in a one-horse open sleigh)
      If you want you can additionally test with some of the new formats in the console:

      require(['core/pending', 'core/str'], function(Pending, Str) {
        M.util.complete_js = []
        M.util.pending_js = []
        console.assert(M.util.pending_js.length === 0, "Nothing pending");
        console.assert(M.util.complete_js.length === 0, "Nothing complete");
       
        let resolver;
        const testPromise = new Promise(resolve => {
          resolver = resolve;
        });
       
        console.assert(M.util.pending_js.length === 0, "Nothing pending");
        console.assert(M.util.complete_js.length === 0, "Nothing complete");
      });
       
      require(['core/pending', 'core/str'], function(Pending, Str) {
        M.util.complete_js = []
        M.util.pending_js = []
        console.assert(M.util.pending_js.length === 0, "Nothing pending");
        console.assert(M.util.complete_js.length === 0, "Nothing complete");
       
        Pending.Promise(resolve => {
          console.assert(M.util.pending_js.length === 1, "pendingPromise");
          console.assert(M.util.complete_js.length === 0, "Nothing complete");
          resolve();
        }).then(() => {
          console.assert(M.util.pending_js.length === 0, "Nothing pending");
          console.assert(M.util.complete_js.length === 1, "Correct number of completed items");
          console.assert(M.util.complete_js[0] === "pendingPromise", "Correct item in complete");
        });
      });
      

      Show
      Behat all the way (in a one-horse open sleigh) If you want you can additionally test with some of the new formats in the console: require(['core/pending', 'core/str'], function(Pending, Str) { M.util.complete_js = [] M.util.pending_js = [] console.assert(M.util.pending_js.length === 0, "Nothing pending"); console.assert(M.util.complete_js.length === 0, "Nothing complete");   let resolver; const testPromise = new Promise(resolve => { resolver = resolve; });   console.assert(M.util.pending_js.length === 0, "Nothing pending"); console.assert(M.util.complete_js.length === 0, "Nothing complete"); });   require(['core/pending', 'core/str'], function(Pending, Str) { M.util.complete_js = [] M.util.pending_js = [] console.assert(M.util.pending_js.length === 0, "Nothing pending"); console.assert(M.util.complete_js.length === 0, "Nothing complete");   Pending.Promise(resolve => { console.assert(M.util.pending_js.length === 1, "pendingPromise"); console.assert(M.util.complete_js.length === 0, "Nothing complete"); resolve(); }).then(() => { console.assert(M.util.pending_js.length === 0, "Nothing pending"); console.assert(M.util.complete_js.length === 1, "Correct number of completed items"); console.assert(M.util.complete_js[0] === "pendingPromise", "Correct item in complete"); }); });

      We should convert the core/pending module to use native Promises.

      This does offer a minor risk because we will no longer be returning a jQuery Deferred promise, but a native Promise. This means that:

      • there is no fail or done function
      • the behaviour on a fail is different to catch

      However, I think that it's worth making this change because the behaviour of jQuery promises is counter-intuitive in some cases.

      This change also introduces some alternate ways to call the code:

      • an observer
      • a native-promise-like syntax

      The observer takes an existing promise:

          const observer = Pending.observe(myExistingPromise, 'Some descriptor (optional)');
      

      The native-promise syntax can be used in the same way as a native Promise:

          Pending.Promise((resolve, reject) => {
              getString('ok')
                  .then(okay => {
                      window.console.log(okay);
                      return okay;
                  })
                  .then(resolve)
                  .catch(reject);
          }, 'mod_myexample/setup:init');
      

            dobedobedoh Andrew Lyons
            dobedobedoh Andrew Lyons
            Tim Hunt Tim Hunt
            Jun Pataleta Jun Pataleta
            CiBoT CiBoT
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 3 hours, 55 minutes
                3h 55m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.