I'm trying to resolve a deferred of a promise after a Q.allSettled
is finished. However, the .then
of the allSettled
never executes and the promise array results are never returned. No exceptions are thrown I just never get inside the .then
block. As you can see from the code below I'm inside of a .then
block iterating over some file upload elements and then executing file uploads and some saves based on those returns.
var deferred = Q.defer();
datacontext.getFileNameGuid(fileNamesNeeded).then(function (data) {
_.each($fileUploads, function(item){
if (item.files.length > 0) {
var promise = uploadFile(item, data[remainingFilesToUpload])
.then(function () {
return datacontext.saveItem(atom)
.then(function (data) {
return getItemSuccess(data, false);
}).done();
}).catch(function (data) {
logger.logError("An error occurred while updating your item. Please try again.", data, 'Error', true);
}).done();
fileUploadPromises.push(promise);
return promise;
}
});
// Either execute promises or bail
if (fileUploadPromises.length > 0) {
// Handle blockUI and spinner in last then.
Q.allSettled(fileUploadPromises).then(function () {
deferred.resolve('Images have been saved and scheduled for thumbnail generation.');
});
} else {
// Other logic here
}
});
return deferred.promise;
After taking advice from @Bergi I refactored a bunch of code that was using the deferred antipattern . I ended up wrapping my code in a Q.fcall block to return the promise and changed Q.allSettled to Q.all. Below is the end state to my working code.
NOTE: There are function calls to Durandal specific functionality and other functions that I've omitted.
return Q.fcall(function () {
try {
var fileUploadPromises = [];
var $fileUploads = $(fileUploadClass);
var fileNamesNeeded = 0;
$fileUploads.each(function (index, item) {
if (item.files.length > 0)
fileNamesNeeded++;
});
return datacontext.getFileNameGuid(fileNamesNeeded).then(function (data) {
_.each($fileUploads, function (item) {
if (item.files.length > 0) {
// Init upload file promise
var promise = uploadFile(item, data[remainingFilesToUpload])
.then(function () {
return datacontext.saveItem(atom)
.then(function (data) {
return getItemSuccess(data, false);
});
}).catch(function (data) {
logger.logError('An error occurred while updating your Item. Please try again.', data, 'Error', true);
app.trigger(config.publishedMessageNames.AtomFileUploaded, "");
});
fileUploadPromises.push(promise);
remainingFilesToUpload++;
}
});
// Either execute promises or bail
if (fileUploadPromises.length > 0) {
return Q.all(fileUploadPromises)
.then(function (results) {
return datacontext.scheduleBatchTask(filesToUpload(), item.DocumentId()).then(function () {
app.trigger(config.publishedMessageNames.FileUploaded, data);
});
}).done();
} else {
app.trigger(config.publishedMessageNames.FileUploaded, "");
}
});
} catch (e) {
return new Error(e);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.