• Nick Stenning's avatar
    Don't mix-and-match Angular and native Promises · 5812fc46
    Nick Stenning authored
    Well this was fun to debug.
    
    Not.
    
    So, it seems that in 1.4 (as opposed to 1.2) Angular Promises are
    special, and don't play well with native browser Promises. In
    particular, Angular promises don't resolve until a digest cycle, which
    means that while this test will pass:
    
        it('should resolve', function (done) {
           var resolved = false;
    
           $timeout(function () { resolved = true; });
           .then(function () {
             assert.isTrue(resolved);
           })
           .then(done, done);
    
           $timeout.flush(); // <- this triggers a digest cycle
        });
    
    This one will not -- Mocha will time it out, because the digest cycle
    triggered by `$timeout.flush()` occurs before the fulfillment handlers
    (with the assertions and the call to `done`) are registered:
    
        it('should resolve', function (done) {
           var resolved = false;
           var wait = $timeout(function () { resolved = true; });
    
           $timeout.flush();
    
           wait.then(function () {
             assert.isTrue(resolved);
           })
           .then(done, done);
        });
    
    If that were the whole story, I would be merely mildly displeased. But
    it's not...
    
    The return value of a call to `.then(onFulfilled, onRejected)` is a
    promise of the return value of the `onFulfilled` callback, which is what
    makes Promises chainable. If `onFulfilled` returns a promise, then that
    is (or should be) the promise returned by `.then(...)`. Unfortunately,
    if we actually do this:
    
        it('should resolve', function (done) {
           $timeout(function () {
              return Promise.resolve('abc');
           })
           .then(function (val) {
             assert.equal(val, 'abc');
           })
           .then(done, done);
    
           $timeout.flush(); // <- this triggers a digest cycle
        });
    
    Then again, Mocha will time out on this test. This is probably because
    the returned Promise resolves on runtime `nextTick`, which is *after*
    the call to `$timeout.flush()`, which means it's after the relevant part
    of the digest cycle which processes the fulfillment handlers for the
    Angular-style promise.
    
    ARGH, Angular. Argh.
    5812fc46
Name
Last commit
Last update
..
annotator Loading commit data...
config Loading commit data...
directive Loading commit data...
filter Loading commit data...
test Loading commit data...
vendor Loading commit data...
annotation-mapper.js Loading commit data...
annotation-sync.coffee Loading commit data...
annotation-ui-controller.coffee Loading commit data...
annotation-ui-sync.coffee Loading commit data...
annotation-ui.coffee Loading commit data...
annotation-viewer-controller.coffee Loading commit data...
app-controller.coffee Loading commit data...
app.coffee Loading commit data...
auth-controller.js Loading commit data...
auth.js Loading commit data...
blocklist.js Loading commit data...
bridge.coffee Loading commit data...
create-group-form.js Loading commit data...
cross-frame.coffee Loading commit data...
discovery.coffee Loading commit data...
drafts.coffee Loading commit data...
events.js Loading commit data...
features.js Loading commit data...
flash.coffee Loading commit data...
form-respond.coffee Loading commit data...
groups.js Loading commit data...
host.coffee Loading commit data...
identity.coffee Loading commit data...
karma-phantomjs-polyfill.js Loading commit data...
karma.config.js Loading commit data...
local-storage.coffee Loading commit data...
permissions.coffee Loading commit data...
pulse.coffee Loading commit data...
query-parser.coffee Loading commit data...
render.coffee Loading commit data...
search-filter.coffee Loading commit data...
session.js Loading commit data...
settings.js Loading commit data...
share-group-form.js Loading commit data...
site.js Loading commit data...
store.js Loading commit data...
stream-controller.coffee Loading commit data...
stream-filter.coffee Loading commit data...
streamer.js Loading commit data...
tags.coffee Loading commit data...
threading.coffee Loading commit data...
time.coffee Loading commit data...
unicode.coffee Loading commit data...
view-filter.coffee Loading commit data...
widget-controller.coffee Loading commit data...