From d099311df03aee7b7527e7ef66de94be306dffc4 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Sat, 7 Oct 2017 11:24:27 -0400 Subject: [PATCH] Use RSVP.Promise.resolve().then(...) to decrement AJAX counters. --- addon-test-support/wait.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/addon-test-support/wait.js b/addon-test-support/wait.js index 1535c2461..8ac61baae 100644 --- a/addon-test-support/wait.js +++ b/addon-test-support/wait.js @@ -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'; @@ -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); } }