Skip to content

Commit

Permalink
Improve cancelation
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 15, 2017
1 parent 75cf6e5 commit c398fc1
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/jest-jasmine2/src/queue_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ function createCancelToken () {
}

function queueRunner(options: Options) {
const token = createCancelToken();

const mapper = ({fn, timeout}) => {
const promise = new Promise(resolve => {
let promise = new Promise(resolve => {
const next = function(err) {
if (err) {
options.fail.apply(null, arguments);
Expand All @@ -55,6 +57,12 @@ function queueRunner(options: Options) {
resolve();
}
});

promise = Promise.race([
promise,
token,
]);

if (!timeout) {
return promise;
}
Expand All @@ -72,14 +80,11 @@ function queueRunner(options: Options) {
},
);
};
const token = createCancelToken();
const returnPromise = Promise.race([
options.queueableFns.reduce(
(promise, fn) => promise.then(() => mapper(fn)),
Promise.resolve(),
),
token,
]);

const returnPromise = options.queueableFns.reduce(
(promise, fn) => promise.then(() => mapper(fn)),
Promise.resolve(),
);

returnPromise.cancel = token.cancel;

Expand Down

0 comments on commit c398fc1

Please sign in to comment.