Javascript Node.js:在事件上运行函数 功能模块(错误){ console.log('Uh-oh:'+err); 过程。退出(1); } 变量框,cmds,next=0,cb=function(err){ 如果(错误) 死(错); else if(下一个
当一封新电子邮件到达('mail',function()时,我希望它再次运行cb()函数。但是,在console.log之后,它不会执行任何操作 我做错了什么Javascript Node.js:在事件上运行函数 功能模块(错误){ console.log('Uh-oh:'+err); 过程。退出(1); } 变量框,cmds,next=0,cb=function(err){ 如果(错误) 死(错); else if(下一个,javascript,node.js,Javascript,Node.js,当一封新电子邮件到达('mail',function()时,我希望它再次运行cb()函数。但是,在console.log之后,它不会执行任何操作 我做错了什么 谢谢重置您的下一个计数器,您的imap.on('mail'),…应该在cmds之外,这样它就不会再次绑定,再次绑定,再次绑定…有一些模块可用于“扁平化”异步操作,以避免在回调地狱中结束 e、 g: 也许这能帮你 function die(err) { console.log('Uh oh: ' + err); process.ex
谢谢重置您的
下一个
计数器,您的imap.on('mail'),…
应该在cmds之外,这样它就不会再次绑定,再次绑定,再次绑定…有一些模块可用于“扁平化”异步操作,以避免在回调地狱中结束
e、 g:
也许这能帮你
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 < 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 };
console.log(msg.headers.date[0]);
console.log(msg.headers.to[0]);
console.log(msg.headers.from[0]);
console.log(msg.headers.subject[0]);
var from = /(.*)?<(.*?)>/.exec(msg.headers.from[0]);
console.log(from[1]); // nome from
console.log(from[2]); // from
});
});
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;
console.log(msg.data);
});
});
fetch.on('end', function() {
console.log('Done fetching bodies!');
cb(undefined, msgCache);
});
});
},
function(msgs) {
// Do something here with msgs, which contains the headers and
// body (parts) of all the messages you fetched
// console.log(msgs);
//imap.logout(cb);
imap.on('mail', function () {
// body...
console.log("New Email Has Arrived!");
next = 0;
cb();
})
}
];
cb();