After I read the question and the corresponding answer, I got on the right path. Thanks, bye! But for the final working permit, I spent another hour for all working cases to work. Therefore, I would like to share an example of code that contains a chain of promises, including an array of promises, to wait for permission.
Use case history - this is importing a server-side file (nodeJs) after loading. I used promises to return the corresponding status and http result.
readFile: function (fileName) { if (fileName) { var deferred = Q.defer(); var self = this; converter({input: fileName}, function (error, userData) { if (error) { deferred.reject(error); } self.storeUsers(error, userData) .then(function (success) { if (success) { deferred.resolve(success) } }) .fail(function (error) { deferred.reject(error) }); }); return deferred.promise; } }, storeUsers: function (error, data) { return Q.all(_.map(data, function (users, emailAddress) { var deferred = Q.defer(); userRepository.findUserByEmail(emailAddress, function (user) {
Hope this helps too!
Cheers Ben
source share