Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
121
rated 0 times [  124] [ 3]  / answers: 1 / hits: 5804  / 9 Years ago, wed, december 16, 2015, 12:00:00

I want to make sure I am not missing a trick; in Kris Kowal's library, you can do the following as a generic catch statement in promises:



var a, b, c, d, e, f;

readFile('fileA')
.then(function (res) {
a = res;

return readFile('fileB');
})
.then(function (res) {
b = res;

return readFile('fileC');
})
.then(function (res) {
c = res;

return readFile('fileD');
})
.then(function (res) {
d = res;

return readFile('fileE');
})
.then(function (res) {
e = res;

return readFile('fileF');
})
.then(function () {
f = res;
})
.catch(function () {
// error happened in file read *somewhere* (don't care where)
});


In jQuery's deferred objects, there is no catch statement, instead, I have to do this:



var a, b, c, d, e, f;

readFile('fileA')
.then(function (res) {
a = res;

return readFile('fileB');
})
.fail(function () {
// error happened in file read *somewhere* (don't care where)
})
.then(function (res) {
b = res;

return readFile('fileC');
})
.fail(function () {
// error happened in file read *somewhere* (don't care where)
})
.then(function (res) {
c = res;

return readFile('fileD');
})
.fail(function () {
// error happened in file read *somewhere* (don't care where)
})
.then(function (res) {
d = res;

return readFile('fileE');
})
.fail(function () {
// error happened in file read *somewhere* (don't care where)
})
.then(function (res) {
e = res;

return readFile('fileF');
})
.fail(function () {
// error happened in file read *somewhere* (don't care where)
})
.then(function (res) {
f = res;

return readFile('fileF');
})
.fail(function () {
// error happened in file read *somewhere* (don't care where)
});


Unfortunately, each then branch has unique logic. Am I missing something, or is the jQuery variation above the only way to achieve the equivalent in Kris Kowal's q library?


More From » jquery

 Answers
2

Assuming readFile returns a promise object, You can actually load all the files asynchronously using $.when() (of cource if you don't care about the order in which files are read):



From the docs:




In the case where multiple Deferred objects are passed to jQuery.when(), the method returns the Promise from a new master Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, the doneCallbacks for the master Deferred are executed. The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to jQuery.when()




(emphasis mine)



$.when(readFile('fileA'), readFile('fileB'), readFile('fileC'), readFile('fileD'), readFile('fileE'), readFile('fileF'))
.then(function(a, b, c, d, e, f) {
// big success
},function() {
// error happened in file read *somewhere* (don't care where)
});

[#32162] Tuesday, December 15, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
;