title: Multiple Q.all inside function?
body: <p>I want to send a list of new books to a user. So far the below code works fine. The problem is that I don't want to send a book multiple times, so I want to filter them. </p>
  
  <p>Current code works fine:</p>
  
  <pre><code>function checkActiveBooks(books) {
    var queue = _(books).map(function(book) {
      var deferred = Q.defer();
  
      // Get all alerts on given keywords
      request('http://localhost:5000/books?l=0&amp;q=' + book.name, function(error, response, body) {
        if (error) {
          deferred.reject(error);
        }
  
        var books = JSON.parse(body);
        if (!_.isEmpty(books)) {
  
          // Loop through users of current book.
          var userBooks = _(book.users).map(function(user) {
  
              // Save object for this user with name and deals.
              return {
                user: user,
                book: book.name,
                books: books
              }
  
          });
  
          if (_.isEmpty(userBooks)) {
             deferred.resolve(null);
           } else {
             deferred.resolve(userBooks);
           }
        } else {
          deferred.resolve(null);
        }
  
      });
  
      return deferred.promise;
  
    });
  
    return Q.all(queue);
  }
  </code></pre>
  
  <p>But now I want to filter already sent books:</p>
  
  <pre><code>function checkActiveBooks(books) {
      var queue = _(books).map(function(book) {
          var deferred = Q.defer();
          // Get all alerts on given keywords
          request('http://localhost:5000/books?l=0&amp;q=' + book.name, function(error, response, body) {
              if (error) {
                  deferred.reject(error);
              }
              var books = JSON.parse(body);
              if (!_.isEmpty(books)) {
                  // Loop through users of current book.
                  var userBooks = _(book.users).map(function(user) {
                      var defer = Q.defer();
                      var userBook = user.userBook.dataValues;
                      // Check per given UserBook which books are already sent to the user by mail
                      checkSentBooks(userBook).then(function(sentBooks) {
                          // Filter books which are already sent.
                          var leftBooks = _.reject(books, function(obj) {
                              return sentBooks.indexOf(obj.id) &gt; -1;
                          });
                          // Save object for this user with name and deals.
                          var result = {
                              user: user,
                              book: book.name,
                              books: leftBooks
                          }
                          return deferred.resolve(result);
                      });
                      return Q.all(userBooks);
                  } else {
                      deferred.resolve(null);
                  }
              });
              return deferred.promise;
          });
          return Q.all(queue);
      }
  </code></pre>
  
  <p>But above code doesn't work. It doesn't stop looping. I thought it made sense to use q.all twice, because it contains two loops. But I guess I'm doing it wrong...</p>