Is there a way to clean the .then instance of JavaScript Promise ?
I wrote a JavaScript test environment on top of QUnit . The structure runs the tests synchronously, running each of them in Promise . (Sorry for the length of this code block. I commented on this as best as possible, so it feels less tiring.)
$$.extend(Promise, { asyncTimeout: function (timeToLive, errorMessage) { var error = new Error(errorMessage || "Operation timed out."); var res,
If the test expires, my promise timeout will be assert.fail() in the test so that the test is marked as unsuccessful, that everything is good and good, but the test continues to work, because the Promise ( result ) test is still waiting for its solution.
I need a good way to cancel my test. I can do this by creating a field in the this.cancelTest frame module or something similar, and checking each so often (for example, at the beginning of each iteration then() ) in the test whether to cancel. However, ideally, I could use $$(at).on("timeout", /* something here */) to clear the remaining then() from my result variable, so that none of the remaining tests will run.
Is there something similar?
Quick update
I tried using Promise.race([result, at.promise]) . This did not work.
Update 2 + Confusion
To unlock me, I added a few lines with mod.cancelTest / polling in the test. (I also deleted the event trigger.)
return new Promise(function (resolve, reject) { console.log("Beginning: " + name); var at = Promise.asyncTimeout(options.timeout, "Test timed out."); at.promise.catch(function () {
I set a breakpoint in the catch expression and hit. What confuses me now is that the then() operator is not being called. Ideas?
Update 3
Thought the last. fn.call() throws an error that I did not catch, so the promise of the test was rejected before at.promise.catch() could solve it.
javascript promise es6-promise cancellation
dx_over_dt Apr 6 '15 at 20:03 2015-04-06 20:03
source share