I have a function that generates some test data and inserts it to a mongodb:
'use strict';
const CvFaker = require('./cv-faker');
const mongoose = require('mongoose');
require('../models/cv_model.js');
module.exports.init = function(){
var cvfaker = new CvFaker();
cvfaker.genCvs(100);
mongoose.model('cv').create(cvfaker.cvs, (err, createdCvs) => {
if(err){
console.log('something went wrong');
}
})
};
I want to execute this code from the mand line:
node -e 'require("./create-mock-db").init()'
The function executes, but it does not wait for the function to plete since it is async. How do I make it wait for the function to plete?
This is not working either:
module.exports.init = function(cb){ ...
..
cb();
node -e 'require("./create-mock-db").init(function(){})'
I have a function that generates some test data and inserts it to a mongodb:
'use strict';
const CvFaker = require('./cv-faker');
const mongoose = require('mongoose');
require('../models/cv_model.js');
module.exports.init = function(){
var cvfaker = new CvFaker();
cvfaker.genCvs(100);
mongoose.model('cv').create(cvfaker.cvs, (err, createdCvs) => {
if(err){
console.log('something went wrong');
}
})
};
I want to execute this code from the mand line:
node -e 'require("./create-mock-db").init()'
The function executes, but it does not wait for the function to plete since it is async. How do I make it wait for the function to plete?
This is not working either:
module.exports.init = function(cb){ ...
..
cb();
node -e 'require("./create-mock-db").init(function(){})'
Share
Improve this question
edited Aug 3, 2016 at 15:21
Joe
asked Aug 3, 2016 at 15:07
JoeJoe
4,25432 gold badges106 silver badges180 bronze badges
9
- In Javascript, functions do not wait for async operations to be done. They just don't. Usually, you use a pletion callback to continue your logic there after the async operation is done. If you describe what actual problem you're trying to solve by waiting, we can probably help you with alternatives. – jfriend00 Commented Aug 3, 2016 at 15:13
- Yes, but how do I add the callback when I'm executing from mandline? – Joe Commented Aug 3, 2016 at 15:19
-
1
Why not create your own script that loads this module and uses a pletion callback as intended. It's hard to write plicated code one line at a time on the console. But, you could pass a callback to
.init()
that gets called when the async operation is done. – jfriend00 Commented Aug 3, 2016 at 15:20 - Since this code should not be in production, only in dev-mode, I don't want to expose it, hence I want to execute it from the mand-line. I don't find any way to do so if you have async code. – Joe Commented Aug 3, 2016 at 15:35
-
It's nuts to do this on the console! But, you can type a function into the console previously so its symbol is defined. Then, pass that symbol as an argument to
.init()
as a callback function. – jfriend00 Commented Aug 3, 2016 at 15:38
3 Answers
Reset to default 3The node
process will not exit until the event queue is empty. The event loop uses the event queue to make asynchronous execution possible.
It's pretty simple to verify that this is not an issue with executing asynchronous code.
node -e "setTimeout(() => console.log('done'), 5000)"
This example takes 5 seconds to run, as you would expect.
The problem with your code is the fact that you never establish a connection with the database. The model.create
method doesn't do anything until there is a connection, therefor nothing ever gets queued and the process is free to exit.
This means your code needs to change to do two things:
- Connect to the database so that the
model.create
method can execute. - Disconnect from the database when
model.create
is plete so the process is free to exit.
As this answer might e up for more people…
// test.js
const request = require('request');
const rp = require('request-promise');
const demo = module.exports.demo = async function() {
try {
const res = await rp.post( {
uri: 'https://httpbin/anything',
body: { hi: 'there', },
}, function (error, response, body) {
return error ? error : body;
} )
console.log( res )
return res;
}
catch ( e ) {
console.error( e );
}
};
Call it like this:
$ node -e 'require("./test").demo()'
Sidenote:
it does not wait for the function to plete since it is async
It's not async. You might call asynchronous functions, but you are not treating them as such and not await
ing any result.
To add to Kaiser's answer, if you are on Windows using cmd, the single/double quotes are important. Put the double quotes on the outside, i.e.
node -e "require('./test').demo()"