Skip to content

Instantly share code, notes, and snippets.

@nomospace
Created August 15, 2012 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomospace/3357290 to your computer and use it in GitHub Desktop.
Save nomospace/3357290 to your computer and use it in GitHub Desktop.
node-imap 依次执行函数队列
var box, cmds, next = 0,
cb = function(err) {
if (err) die(err);
else if (next < cmds.length) cmds[next++].apply(this, Array.prototype.slice.call(arguments).slice(1));
};
cmds = [
function() {
imap.connect(cb);
}, function() {
imap.openBox('INBOX', false, cb);
}, function(result) {
box = result;
imap.search(['SEEN', ['SINCE', 'August 15, 2012']], cb);
}, function(results) {
var fetch = imap.fetch(results, {
request: {
headers: ['from', 'to', 'subject', 'date']
}
});
fetch.on('message', function(msg) {
console.log('Got message: ' + util.inspect(msg, true, 5));
msg.on('data', function(chunk) {
console.log('Got message chunk of size ' + chunk.length);
});
msg.on('end', function() {
console.log('Finished message: ' + util.inspect(msg, false, 5));
});
});
fetch.on('end', function() {
console.log('Done fetching all messages!');
imap.logout(cb);
});
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment