.then
जावास्क्रिप्ट Promise
उदाहरण के समाशोधन के लिए कोई विधि है ?
मैंने क्विंट के शीर्ष पर जावास्क्रिप्ट परीक्षण रूपरेखा लिखी है । फ्रेमवर्क एक-एक करके सभी को एक-एक करके चलाता है Promise
। (इस कोड ब्लॉक की लंबाई के लिए खेद है। मैंने इसे सर्वश्रेष्ठ के रूप में टिप्पणी की, इसलिए यह कम थकाऊ लगता है।)
/* Promise extension -- used for easily making an async step with a
timeout without the Promise knowing anything about the function
it's waiting on */
$$.extend(Promise, {
asyncTimeout: function (timeToLive, errorMessage) {
var error = new Error(errorMessage || "Operation timed out.");
var res, // resolve()
rej, // reject()
t, // timeout instance
rst, // reset timeout function
p, // the promise instance
at; // the returned asyncTimeout instance
function createTimeout(reject, tempTtl) {
return setTimeout(function () {
// triggers a timeout event on the asyncTimeout object so that,
// if we want, we can do stuff outside of a .catch() block
// (may not be needed?)
$$(at).trigger("timeout");
reject(error);
}, tempTtl || timeToLive);
}
p = new Promise(function (resolve, reject) {
if (timeToLive != -1) {
t = createTimeout(reject);
// reset function -- allows a one-time timeout different
// from the one original specified
rst = function (tempTtl) {
clearTimeout(t);
t = createTimeout(reject, tempTtl);
}
} else {
// timeToLive = -1 -- allow this promise to run indefinitely
// used while debugging
t = 0;
rst = function () { return; };
}
res = function () {
clearTimeout(t);
resolve();
};
rej = reject;
});
return at = {
promise: p,
resolve: res,
reject: rej,
reset: rst,
timeout: t
};
}
});
/* framework module members... */
test: function (name, fn, options) {
var mod = this; // local reference to framework module since promises
// run code under the window object
var defaultOptions = {
// default max running time is 5 seconds
timeout: 5000
}
options = $$.extend({}, defaultOptions, options);
// remove timeout when debugging is enabled
options.timeout = mod.debugging ? -1 : options.timeout;
// call to QUnit.test()
test(name, function (assert) {
// tell QUnit this is an async test so it doesn't run other tests
// until done() is called
var done = assert.async();
return new Promise(function (resolve, reject) {
console.log("Beginning: " + name);
var at = Promise.asyncTimeout(options.timeout, "Test timed out.");
$$(at).one("timeout", function () {
// assert.fail() is just an extension I made that literally calls
// assert.ok(false, msg);
assert.fail("Test timed out");
});
// run test function
var result = fn.call(mod, assert, at.reset);
// if the test returns a Promise, resolve it before resolving the test promise
if (result && result.constructor === Promise) {
// catch unhandled errors thrown by the test so future tests will run
result.catch(function (error) {
var msg = "Unhandled error occurred."
if (error) {
msg = error.message + "\n" + error.stack;
}
assert.fail(msg);
}).then(function () {
// resolve the timeout Promise
at.resolve();
resolve();
});
} else {
// if test does not return a Promise, simply clear the timeout
// and resolve our test Promise
at.resolve();
resolve();
}
}).then(function () {
// tell QUnit that the test is over so that it can clean up and start the next test
done();
console.log("Ending: " + name);
});
});
}
यदि कोई परीक्षण समय समाप्त हो जाता है, तो मेरा टाइमआउट वादा assert.fail()
परीक्षण पर होगा , ताकि परीक्षण विफल के रूप में चिह्नित हो, जो सभी अच्छी तरह से और अच्छा है, लेकिन परीक्षण जारी है क्योंकि परीक्षण का वादा ( result
) अभी भी इसे हल करने के लिए इंतजार कर रहा है।
मुझे अपना टेस्ट रद्द करने के लिए एक अच्छे तरीके की जरूरत है। मैं इसे फ्रेमवर्क मॉड्यूल this.cancelTest
या कुछ पर एक फ़ील्ड बनाकर कर सकता हूं , और हर बार जांच कर सकता हूं (उदाहरण के लिए प्रत्येक then()
पुनरावृत्ति की शुरुआत में ) कि क्या रद्द करना है। हालाँकि, आदर्श रूप से, मैं अपने चर पर $$(at).on("timeout", /* something here */)
शेष then()
s को साफ़ करने के लिए उपयोग कर सकता हूं result
, ताकि बाकी परीक्षण में से कोई भी न चले।
क्या ऐसा कुछ मौजूद है?
शीघ्र नवीनीकरण
मैंने प्रयोग करने की कोशिश की Promise.race([result, at.promise])
। यह काम नहीं किया।
अद्यतन 2 + भ्रम
मुझे अनवरोधित करने के लिए, मैंने mod.cancelTest
परीक्षण विचार के भीतर / मतदान के साथ कुछ पंक्तियाँ जोड़ीं। (मैंने इवेंट ट्रिगर भी हटा दिया है।)
return new Promise(function (resolve, reject) {
console.log("Beginning: " + name);
var at = Promise.asyncTimeout(options.timeout, "Test timed out.");
at.promise.catch(function () {
// end the test if it times out
mod.cancelTest = true;
assert.fail("Test timed out");
resolve();
});
// ...
}).then(function () {
// tell QUnit that the test is over so that it can clean up and start the next test
done();
console.log("Ending: " + name);
});
मैंने catch
बयान में एक विराम बिंदु निर्धारित किया है , और यह हिट हो रहा है। अब जो मुझे भ्रमित कर रहा है वह यह है कि then()
बयान को नहीं बुलाया जा रहा है। विचार?
अपडेट ३
आखिरी बात का पता लगाया। fn.call()
एक त्रुटि फेंक रहा था जिसे मैंने नहीं पकड़ा, इसलिए परीक्षण का वादा at.promise.catch()
इसे हल करने से पहले अस्वीकार कर रहा था ।