Add support for limit when searching #221

engram-design opened this Issue May 31, 2013 · 3 comments


None yet
2 participants

Related to #218 would it be possible to add a LIMIT option when searching for emails? I'm currently using [ 'ALL', ['SINCE', 'yesterday'] ], and processing those emails. The issue is - what if the script has missed processing emails before yesterday?

To alleviate the errors produced in #218 I need to not read so many emails per batch, which would be really easy to do with LIMIT - and wouldn't be constrained to time.



mscdex commented May 31, 2013

There is no built-in mechanism in the imap protocol (or even any extra server capabilities that I know of) to limit search results.

I think the best way is to just fetch a portion of the results array at a time (you might have to use some trial and error to find out what the optimal size is). One solution might be something like (untested):

function fetchMsgs(results, idx, cb) {
  if (typeof idx === 'function') {
    cb = idx;
    idx = 0;
  var end = Math.min(results.length, idx + FETCH_CONCURRENCY);
  conn.fetch(results.slice(idx, end), {
    // ....
  }, function(err) {
    if (err) return cb(err);
    if (end < results.length)
      fechMsgs(results, end, cb);

// somewhere else in your code after performing your search
// and storing results in `results`...
fetchMsgs(results, function(err) {
  if (err) throw err; // something more sensible than this obviously
  // all fetches done!

@mscdex Yep, I think that's a fine solution! Only reason I suggested limit was mainly from the feature here


mscdex commented May 31, 2013

@engram-design The limit being used there is just a simple range based on messages' sequence numbers, which you can do in this module as well with search(). The problem is you may not know what the sequence numbers are before searching. If you did know them before searching, then you probably wouldn't have to search in the first place... :-)

@mscdex mscdex closed this Jun 28, 2013

@chadxz chadxz referenced this issue in chadxz/imap-simple Mar 31, 2017


how to limit the amount of messages #33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment