My program is exiting before the function is even completed. I have also used callback but still the function is exiting.
var Imap = require('imap'),
inspect = require('util').inspect;
var MailParser = require("mailparser").MailParser;
var mail, otp, imap;
var fullPattern = new RegExp('<span.*>[0-9]{6}</span>');
var otpPattern = new RegExp('[0-9]{6}');
function setEmail(email, password, mailType){
if(mailType == 'outlook')
mail = outlook;
else
mail = gmail;
mail.email = email;
mail.username = email;
mail.password = password;
}
function setIMAP(){
imap = new Imap({
user: mail.email,
password: mail.password,
host: mail.imap.host,
port: 993,
tls: true
});
}
function read(email, password, mailType){
setEmail(email, password, mailType);
setIMAP();
readMail(function(err, otp){
if(err){
console.log(err);
return null;
}
else{
if(otp != undefined){
console.log('Final OTP : ' + otp);
return otp;
}
else{
console.log('Sorry No OTP found ');
return null;
}
}
});
}
function openInbox(cb) {
imap.openBox('INBOX', false, cb);
}
function readMail(callback){
imap.once('ready', function(){
openInbox(function(err, box){
if(err) throw err;
imap.search(['UNSEEN'], function(err, results){
if(results){
console.log('No new messages');
callback('NO_MSG', null);
}else{
imap.setFlags(results, ['\\Seen'], function(err) {
if (!err) {
console.log("marked as read");
} else {
console.log(JSON.stringify(err, null, 2));
}
});
var f = imap.fetch(results, {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
struct: true
});
f.on('message', function(msg, seqno){
var msgHeaders = '', msgText = '';
console.log("Processing msg #" + seqno);
var parser = new MailParser();
parser.on('data', data => {
msgText = data.text;
if (data.type === 'text') {
var spanTag = fullPattern.exec(data.text);
console.log('Span : ' + spanTag);
var currOtp = otpPattern.exec(spanTag);
if(spanTag != '' && spanTag != null && spanTag != 'null')
otp = currOtp;
console.log('OTP : ' + otp);
}
});
msg.on("body", function(stream, info) {
stream.on("data", function(chunk) {
parser.write(chunk.toString('utf8'));
});
});
msg.once("end", function() {
console.log('Ending ' + seqno);
parser.end();
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
imap.end();
});
}
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
console.log('Main OTP : ' + otp);
callback(null, otp);
});
imap.connect();
}
function run(){
var finalOtp = read('*******@******.com', '*****', 'outlook');
console.log(finalOtp);
}
I am able to read the mails and get the desired OTP value. But the program is exiting but the function is still executing. the run() function is exiting by printing undefined but after a second finalOTP gets correct value. I am not able to understand how do I stop the function to exit generally.
Assuming if I have a no code below the read() calling statement. The function is directly exiting but after a while some data is being printed later.