nodejs async await/async not working as expected
This is my code, it is supposed to get the query result back from findRecords function before execute the next line.
const FsPool = module.exports = function(dir)
events.EventEmitter.call(this)
this.dir = dirpath;
this.files = ;
this.active = ;
this.threads = 1;
this.on('run', this.runQuta.bind(this))
;
// So will act like an event emitter
util.inherits(FsPool, events.EventEmitter);
FsPool.prototype.runQuta = function()
if (this.files.length === 0 && this.active.length === 0)
return this.emit('done');
if (this.active.length < this.threads)
const name = this.files.shift()
this.active.push(name)
const fileName = path.join(this.dir, name);
const self = this;
fs.stat(fileName, function(err, stats)
if (err)
throw err;
if (stats.isFile())
fs.readFile(fileName, function(err, data)
if (err)
throw err;
self.active.splice(self.active.indexOf(name), 1)
self.emit('file', name, data);
self.emit('run');
);
else
self.active.splice(self.active.indexOf(name), 1)
self.emit('dir', name);
self.emit('run');
);
return this
;
FsPool.prototype.init = function()
const dir = dirpath;
const self = this;
fs.readdir(dir, function(err, files)
if (err)
throw err;
self.files = files
self.emit('run');
)
return this
;
const fsPool = new FsPool(__dirname)
fsPool.on('file', async function(fileName, fileData)
//console.log('file name: ' + fileName);
//console.log('file data: ', fileData.toString('utf8'));
let nctid = fileName.split('.');
nctid = nctid[0];
console.log(nctid);
getData(fileData.toString('utf8'), function(returnValue)
fs.writeFile(fpath + nctid + '.json', returnValue.trim(), 'utf8', function (err)
if (err)
return console.log(err);
console.log("The file was saved!");
);
);
})
fsPool.on('dir', function(dirName)
console.log('dir name: ' + dirName);
)
fsPool.on('done', function()
console.log('done');
);
const findRecords = (collection, myobj) =>
return new Promise((resolve, reject) =>
collection.find(myobj).toArray((err, data) =>
if (err)
console.error(`Cannot find records: $err`)
reject('error');
else
console.log(`Record count: $data.length`)
resolve(data)
)
);
function getData(data, callback)
let jsonO =
"results": ,
"Spots": ,
"SpotsC":
;
let jsonspot = ;
let jsonspotC = ;
let json = ;
json.push();
const main = async () =>
let readFileContent = JSON.parse(JSON.stringify(parseXML(data)));
readFileContent = readFileContent.clinical_study;
//Spot (city level) and SpotC (country level)
let lengthdata = 1;
let country = '';
let city = '';
let state = '';
let name = '';
let keyidx = 0;
if (readFileContent.hasOwnProperty('location'))
for (let c = 0, len = lengthdata; c < len; c++) ' + state + '
jsonO["results"] = json;
jsonO["Spots"] = jsonspot;
jsonO["SpotsC"] = jsonspotC;
callback(JSON.stringify(jsonO));
main().catch(console.error);
Here is the result I got:
00000102|United States|South Carolina|Charleston
00000104|United States|Minnesota|Minneapolis
00000105|United States|Minnesota|Minneapolis
00000106|United States|Wisconsin|Madison
00000107|United States|Vermont|Burlington
00000108|United States|Michigan|Ann Arbor
Record count: 1
latlngDB:1 Info:^United States$ ^South Carolina$ ^Charleston$
Record count: 1
latlngDB:1 Info:^United States$ ^Minnesota$ ^Minneapolis$
Record count: 1
latlngDB:1 Info:^United States$ ^Minnesota$ ^Minneapolis$
Record count: 1
latlngDB:1 Info:^United States$ ^Michigan$ ^Ann Arbor$
NCT00000110|United States|Alabama|Birmingham
Record count: 1
latlngDB:1 Info:^United States$ ^Wisconsin$ ^Madison$
console.log(readFileContent.id_info.id + '|' + country + '|' + state + '|' + city); //<-- This line is keep on printing without waiting for the next line to be complete. Async/Await not working ???
node.js async-await
add a comment |
This is my code, it is supposed to get the query result back from findRecords function before execute the next line.
const FsPool = module.exports = function(dir)
events.EventEmitter.call(this)
this.dir = dirpath;
this.files = ;
this.active = ;
this.threads = 1;
this.on('run', this.runQuta.bind(this))
;
// So will act like an event emitter
util.inherits(FsPool, events.EventEmitter);
FsPool.prototype.runQuta = function()
if (this.files.length === 0 && this.active.length === 0)
return this.emit('done');
if (this.active.length < this.threads)
const name = this.files.shift()
this.active.push(name)
const fileName = path.join(this.dir, name);
const self = this;
fs.stat(fileName, function(err, stats)
if (err)
throw err;
if (stats.isFile())
fs.readFile(fileName, function(err, data)
if (err)
throw err;
self.active.splice(self.active.indexOf(name), 1)
self.emit('file', name, data);
self.emit('run');
);
else
self.active.splice(self.active.indexOf(name), 1)
self.emit('dir', name);
self.emit('run');
);
return this
;
FsPool.prototype.init = function()
const dir = dirpath;
const self = this;
fs.readdir(dir, function(err, files)
if (err)
throw err;
self.files = files
self.emit('run');
)
return this
;
const fsPool = new FsPool(__dirname)
fsPool.on('file', async function(fileName, fileData)
//console.log('file name: ' + fileName);
//console.log('file data: ', fileData.toString('utf8'));
let nctid = fileName.split('.');
nctid = nctid[0];
console.log(nctid);
getData(fileData.toString('utf8'), function(returnValue)
fs.writeFile(fpath + nctid + '.json', returnValue.trim(), 'utf8', function (err)
if (err)
return console.log(err);
console.log("The file was saved!");
);
);
})
fsPool.on('dir', function(dirName)
console.log('dir name: ' + dirName);
)
fsPool.on('done', function()
console.log('done');
);
const findRecords = (collection, myobj) =>
return new Promise((resolve, reject) =>
collection.find(myobj).toArray((err, data) =>
if (err)
console.error(`Cannot find records: $err`)
reject('error');
else
console.log(`Record count: $data.length`)
resolve(data)
)
);
function getData(data, callback)
let jsonO =
"results": ,
"Spots": ,
"SpotsC":
;
let jsonspot = ;
let jsonspotC = ;
let json = ;
json.push();
const main = async () =>
let readFileContent = JSON.parse(JSON.stringify(parseXML(data)));
readFileContent = readFileContent.clinical_study;
//Spot (city level) and SpotC (country level)
let lengthdata = 1;
let country = '';
let city = '';
let state = '';
let name = '';
let keyidx = 0;
if (readFileContent.hasOwnProperty('location'))
for (let c = 0, len = lengthdata; c < len; c++) ' + state + '
jsonO["results"] = json;
jsonO["Spots"] = jsonspot;
jsonO["SpotsC"] = jsonspotC;
callback(JSON.stringify(jsonO));
main().catch(console.error);
Here is the result I got:
00000102|United States|South Carolina|Charleston
00000104|United States|Minnesota|Minneapolis
00000105|United States|Minnesota|Minneapolis
00000106|United States|Wisconsin|Madison
00000107|United States|Vermont|Burlington
00000108|United States|Michigan|Ann Arbor
Record count: 1
latlngDB:1 Info:^United States$ ^South Carolina$ ^Charleston$
Record count: 1
latlngDB:1 Info:^United States$ ^Minnesota$ ^Minneapolis$
Record count: 1
latlngDB:1 Info:^United States$ ^Minnesota$ ^Minneapolis$
Record count: 1
latlngDB:1 Info:^United States$ ^Michigan$ ^Ann Arbor$
NCT00000110|United States|Alabama|Birmingham
Record count: 1
latlngDB:1 Info:^United States$ ^Wisconsin$ ^Madison$
console.log(readFileContent.id_info.id + '|' + country + '|' + state + '|' + city); //<-- This line is keep on printing without waiting for the next line to be complete. Async/Await not working ???
node.js async-await
Can you provide the findRecords function implementation ? The problem is probably inside it
– Jonathan Muller
Nov 13 '18 at 2:10
Sorry forgot to add that. Added now
– user3736228
Nov 13 '18 at 2:16
Any idea on this please?
– user3736228
Nov 13 '18 at 4:15
I think there's something else going on here that you're not showing us because theawaityou're asking about should pause theforloop just fine as long as you're awaiting a promise.
– jfriend00
Nov 13 '18 at 4:36
Apologize. i have included all the code now. Please look at the function fsPool.on('file', async function(fileName, fileData) that is calling getData function
– user3736228
Nov 13 '18 at 5:01
add a comment |
This is my code, it is supposed to get the query result back from findRecords function before execute the next line.
const FsPool = module.exports = function(dir)
events.EventEmitter.call(this)
this.dir = dirpath;
this.files = ;
this.active = ;
this.threads = 1;
this.on('run', this.runQuta.bind(this))
;
// So will act like an event emitter
util.inherits(FsPool, events.EventEmitter);
FsPool.prototype.runQuta = function()
if (this.files.length === 0 && this.active.length === 0)
return this.emit('done');
if (this.active.length < this.threads)
const name = this.files.shift()
this.active.push(name)
const fileName = path.join(this.dir, name);
const self = this;
fs.stat(fileName, function(err, stats)
if (err)
throw err;
if (stats.isFile())
fs.readFile(fileName, function(err, data)
if (err)
throw err;
self.active.splice(self.active.indexOf(name), 1)
self.emit('file', name, data);
self.emit('run');
);
else
self.active.splice(self.active.indexOf(name), 1)
self.emit('dir', name);
self.emit('run');
);
return this
;
FsPool.prototype.init = function()
const dir = dirpath;
const self = this;
fs.readdir(dir, function(err, files)
if (err)
throw err;
self.files = files
self.emit('run');
)
return this
;
const fsPool = new FsPool(__dirname)
fsPool.on('file', async function(fileName, fileData)
//console.log('file name: ' + fileName);
//console.log('file data: ', fileData.toString('utf8'));
let nctid = fileName.split('.');
nctid = nctid[0];
console.log(nctid);
getData(fileData.toString('utf8'), function(returnValue)
fs.writeFile(fpath + nctid + '.json', returnValue.trim(), 'utf8', function (err)
if (err)
return console.log(err);
console.log("The file was saved!");
);
);
})
fsPool.on('dir', function(dirName)
console.log('dir name: ' + dirName);
)
fsPool.on('done', function()
console.log('done');
);
const findRecords = (collection, myobj) =>
return new Promise((resolve, reject) =>
collection.find(myobj).toArray((err, data) =>
if (err)
console.error(`Cannot find records: $err`)
reject('error');
else
console.log(`Record count: $data.length`)
resolve(data)
)
);
function getData(data, callback)
let jsonO =
"results": ,
"Spots": ,
"SpotsC":
;
let jsonspot = ;
let jsonspotC = ;
let json = ;
json.push();
const main = async () =>
let readFileContent = JSON.parse(JSON.stringify(parseXML(data)));
readFileContent = readFileContent.clinical_study;
//Spot (city level) and SpotC (country level)
let lengthdata = 1;
let country = '';
let city = '';
let state = '';
let name = '';
let keyidx = 0;
if (readFileContent.hasOwnProperty('location'))
for (let c = 0, len = lengthdata; c < len; c++) ' + state + '
jsonO["results"] = json;
jsonO["Spots"] = jsonspot;
jsonO["SpotsC"] = jsonspotC;
callback(JSON.stringify(jsonO));
main().catch(console.error);
Here is the result I got:
00000102|United States|South Carolina|Charleston
00000104|United States|Minnesota|Minneapolis
00000105|United States|Minnesota|Minneapolis
00000106|United States|Wisconsin|Madison
00000107|United States|Vermont|Burlington
00000108|United States|Michigan|Ann Arbor
Record count: 1
latlngDB:1 Info:^United States$ ^South Carolina$ ^Charleston$
Record count: 1
latlngDB:1 Info:^United States$ ^Minnesota$ ^Minneapolis$
Record count: 1
latlngDB:1 Info:^United States$ ^Minnesota$ ^Minneapolis$
Record count: 1
latlngDB:1 Info:^United States$ ^Michigan$ ^Ann Arbor$
NCT00000110|United States|Alabama|Birmingham
Record count: 1
latlngDB:1 Info:^United States$ ^Wisconsin$ ^Madison$
console.log(readFileContent.id_info.id + '|' + country + '|' + state + '|' + city); //<-- This line is keep on printing without waiting for the next line to be complete. Async/Await not working ???
node.js async-await
This is my code, it is supposed to get the query result back from findRecords function before execute the next line.
const FsPool = module.exports = function(dir)
events.EventEmitter.call(this)
this.dir = dirpath;
this.files = ;
this.active = ;
this.threads = 1;
this.on('run', this.runQuta.bind(this))
;
// So will act like an event emitter
util.inherits(FsPool, events.EventEmitter);
FsPool.prototype.runQuta = function()
if (this.files.length === 0 && this.active.length === 0)
return this.emit('done');
if (this.active.length < this.threads)
const name = this.files.shift()
this.active.push(name)
const fileName = path.join(this.dir, name);
const self = this;
fs.stat(fileName, function(err, stats)
if (err)
throw err;
if (stats.isFile())
fs.readFile(fileName, function(err, data)
if (err)
throw err;
self.active.splice(self.active.indexOf(name), 1)
self.emit('file', name, data);
self.emit('run');
);
else
self.active.splice(self.active.indexOf(name), 1)
self.emit('dir', name);
self.emit('run');
);
return this
;
FsPool.prototype.init = function()
const dir = dirpath;
const self = this;
fs.readdir(dir, function(err, files)
if (err)
throw err;
self.files = files
self.emit('run');
)
return this
;
const fsPool = new FsPool(__dirname)
fsPool.on('file', async function(fileName, fileData)
//console.log('file name: ' + fileName);
//console.log('file data: ', fileData.toString('utf8'));
let nctid = fileName.split('.');
nctid = nctid[0];
console.log(nctid);
getData(fileData.toString('utf8'), function(returnValue)
fs.writeFile(fpath + nctid + '.json', returnValue.trim(), 'utf8', function (err)
if (err)
return console.log(err);
console.log("The file was saved!");
);
);
})
fsPool.on('dir', function(dirName)
console.log('dir name: ' + dirName);
)
fsPool.on('done', function()
console.log('done');
);
const findRecords = (collection, myobj) =>
return new Promise((resolve, reject) =>
collection.find(myobj).toArray((err, data) =>
if (err)
console.error(`Cannot find records: $err`)
reject('error');
else
console.log(`Record count: $data.length`)
resolve(data)
)
);
function getData(data, callback)
let jsonO =
"results": ,
"Spots": ,
"SpotsC":
;
let jsonspot = ;
let jsonspotC = ;
let json = ;
json.push();
const main = async () =>
let readFileContent = JSON.parse(JSON.stringify(parseXML(data)));
readFileContent = readFileContent.clinical_study;
//Spot (city level) and SpotC (country level)
let lengthdata = 1;
let country = '';
let city = '';
let state = '';
let name = '';
let keyidx = 0;
if (readFileContent.hasOwnProperty('location'))
for (let c = 0, len = lengthdata; c < len; c++) ' + state + '
jsonO["results"] = json;
jsonO["Spots"] = jsonspot;
jsonO["SpotsC"] = jsonspotC;
callback(JSON.stringify(jsonO));
main().catch(console.error);
Here is the result I got:
00000102|United States|South Carolina|Charleston
00000104|United States|Minnesota|Minneapolis
00000105|United States|Minnesota|Minneapolis
00000106|United States|Wisconsin|Madison
00000107|United States|Vermont|Burlington
00000108|United States|Michigan|Ann Arbor
Record count: 1
latlngDB:1 Info:^United States$ ^South Carolina$ ^Charleston$
Record count: 1
latlngDB:1 Info:^United States$ ^Minnesota$ ^Minneapolis$
Record count: 1
latlngDB:1 Info:^United States$ ^Minnesota$ ^Minneapolis$
Record count: 1
latlngDB:1 Info:^United States$ ^Michigan$ ^Ann Arbor$
NCT00000110|United States|Alabama|Birmingham
Record count: 1
latlngDB:1 Info:^United States$ ^Wisconsin$ ^Madison$
console.log(readFileContent.id_info.id + '|' + country + '|' + state + '|' + city); //<-- This line is keep on printing without waiting for the next line to be complete. Async/Await not working ???
node.js async-await
node.js async-await
edited Nov 13 '18 at 5:00
user3736228
asked Nov 13 '18 at 2:07
user3736228user3736228
847
847
Can you provide the findRecords function implementation ? The problem is probably inside it
– Jonathan Muller
Nov 13 '18 at 2:10
Sorry forgot to add that. Added now
– user3736228
Nov 13 '18 at 2:16
Any idea on this please?
– user3736228
Nov 13 '18 at 4:15
I think there's something else going on here that you're not showing us because theawaityou're asking about should pause theforloop just fine as long as you're awaiting a promise.
– jfriend00
Nov 13 '18 at 4:36
Apologize. i have included all the code now. Please look at the function fsPool.on('file', async function(fileName, fileData) that is calling getData function
– user3736228
Nov 13 '18 at 5:01
add a comment |
Can you provide the findRecords function implementation ? The problem is probably inside it
– Jonathan Muller
Nov 13 '18 at 2:10
Sorry forgot to add that. Added now
– user3736228
Nov 13 '18 at 2:16
Any idea on this please?
– user3736228
Nov 13 '18 at 4:15
I think there's something else going on here that you're not showing us because theawaityou're asking about should pause theforloop just fine as long as you're awaiting a promise.
– jfriend00
Nov 13 '18 at 4:36
Apologize. i have included all the code now. Please look at the function fsPool.on('file', async function(fileName, fileData) that is calling getData function
– user3736228
Nov 13 '18 at 5:01
Can you provide the findRecords function implementation ? The problem is probably inside it
– Jonathan Muller
Nov 13 '18 at 2:10
Can you provide the findRecords function implementation ? The problem is probably inside it
– Jonathan Muller
Nov 13 '18 at 2:10
Sorry forgot to add that. Added now
– user3736228
Nov 13 '18 at 2:16
Sorry forgot to add that. Added now
– user3736228
Nov 13 '18 at 2:16
Any idea on this please?
– user3736228
Nov 13 '18 at 4:15
Any idea on this please?
– user3736228
Nov 13 '18 at 4:15
I think there's something else going on here that you're not showing us because the
await you're asking about should pause the for loop just fine as long as you're awaiting a promise.– jfriend00
Nov 13 '18 at 4:36
I think there's something else going on here that you're not showing us because the
await you're asking about should pause the for loop just fine as long as you're awaiting a promise.– jfriend00
Nov 13 '18 at 4:36
Apologize. i have included all the code now. Please look at the function fsPool.on('file', async function(fileName, fileData) that is calling getData function
– user3736228
Nov 13 '18 at 5:01
Apologize. i have included all the code now. Please look at the function fsPool.on('file', async function(fileName, fileData) that is calling getData function
– user3736228
Nov 13 '18 at 5:01
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272740%2fnodejs-async-await-async-not-working-as-expected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272740%2fnodejs-async-await-async-not-working-as-expected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you provide the findRecords function implementation ? The problem is probably inside it
– Jonathan Muller
Nov 13 '18 at 2:10
Sorry forgot to add that. Added now
– user3736228
Nov 13 '18 at 2:16
Any idea on this please?
– user3736228
Nov 13 '18 at 4:15
I think there's something else going on here that you're not showing us because the
awaityou're asking about should pause theforloop just fine as long as you're awaiting a promise.– jfriend00
Nov 13 '18 at 4:36
Apologize. i have included all the code now. Please look at the function fsPool.on('file', async function(fileName, fileData) that is calling getData function
– user3736228
Nov 13 '18 at 5:01