Skip to content

Commit

Permalink
Use RSVP.Promise.resolve().then(...) to decrement AJAX counters.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Oct 7, 2017
1 parent 29711bf commit d099311
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions addon-test-support/wait.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* globals self */

import { next, run } from '@ember/runloop';
import { run } from '@ember/runloop';

import { Promise as EmberPromise } from 'rsvp';
import jQuery from 'jquery';
Expand All @@ -13,9 +13,18 @@ function incrementAjaxPendingRequests(_, xhr) {
}

function decrementAjaxPendingRequests(_, xhr) {
next(() => {
// In most Ember versions to date (current version is 2.16) RSVP promises are
// configured to flush in the actions queue of the Ember run loop, however it
// is possible that in the future this changes to use "true" micro-task
// queues.
//
// The entire point here, is that _whenever_ promises are resolved, this
// counter will decrement. In the specific case of AJAX, this means that any
// promises chained off of `$.ajax` will properly have their `.then` called
// _before_ this is decremented (and testing continues)
EmberPromise.resolve().then(() => {
for (var i = 0; i < requests.length; i++) {
if (xhr === requests[i]) {
if (xhr === requests[i]) {
requests.splice(i, 1);
}
}
Expand Down

0 comments on commit d099311

Please sign in to comment.