title: Node.js: Callback is returning an fault
body: <pre><code>function die(err) {
    console.log('Uh oh: ' + err);
    process.exit(1);
  }
  
  var box, cmds, next = 0, cb = function(err) {
    if (err)
      die(err);
    else if (next &lt; 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([ 'UNSEEN', ['SINCE', 'April 5, 2011'] ], cb); },
    function(results) {
      var msgCache = {},
          fetch = imap.fetch(results, { request: { headers: ['from', 'to', 'subject', 'date'] } });
      console.log('Now fetching headers!');
      fetch.on('message', function(msg) {
        msg.on('end', function() {
          msgCache[msg.id] = { headers: msg.headers };
        });
      });
      fetch.on('end', function() {
        console.log('Done fetching headers!');
        console.log('Now fetching bodies!');
        fetch = imap.fetch(results, { request: { headers: false, body: '1' } });
        fetch.on('message', function(msg) {
          msg.data = '';
          msg.on('data', function(chunk) {
            msg.data += chunk;
          });
          msg.on('end', function() {
            msgCache[msg.id].body = msg.data;
          });
        });
        fetch.on('end', function() {
          console.log('Done fetching bodies!');
          cb(msgCache);
        });
      });
    },
    function(msgs) {
      // Do something here with msgs, which contains the headers and
      // body (parts) of all the messages you fetched
      console.log(&quot;HERE&quot;);
      imap.logout(cb);    
      
    }
  ];
  cb();
  </code></pre>
  <p>I'm using that code to get the headers and the body of an e-mail. However, when I try to pass the result (msgCache) it gets an error.</p>
  <p>What am I missing here?</p>