Export promise/asynchronous function (result) node.js through the module.exports?

Hi!

So, the question that certainly rises with each new version of JS, and almost everyone who experienced something asynchronous to node.js.

There is a code module (in brief):
function name1 (parametrizable) {
 request (options)
 .then(response => {
 let charset = [];
 charset.name = response.data.
 //here the code that does something
 return (charset);
})
 .then(charset => {
 if (charset.value=== undefined) {
 charset.value= 'PR10';
 } else {
 charset.guild = charset.guild.name;
}

 return charset; //what I need to in app.js
})
 .catch(function (error) {
console.log('error')
});
}
module.exports = name1

Is app.js where we have a mongoDB using mongoose:
const trade_log = require("./db/models/trade_log");
const name1 = require("./db/ops/module");
app.all('/log', function (req, res, next) { //this we Express
 let x = name1(req.body.Counterparty); //here I have problem
 trade_log.create({ //here we write to the database
 Flag: req.body.Flag
 Instrument: req.body.Instrument,
 Venue: req.body.Venue
 Price: req.body.Price,
 Currency: req.body.Currency,
 Quantity: req.body.Quantity
 Counterparty: req.body.Counterparty,
 Identifier: x
 Commentary: req.body.Commentary,
 },function (err, res) {
 if (err) return console.log (x) + handleError(err);
console.log(res);
});
next();
});


The problem is that I can't seem to export the result of the function and write it to a variable. After a day of hupcoming, I understand this is a problem which lies in the plane of the async/await features and promise. Because. when I try (and tried before) to export a function from your module I get so. what:
  • or x returns 'undefind' i.e. it is obvious that the code runs fine by itself, simply because of asynchronnimi, the answer to the query arrives longer than going to drink in MongoDB and the variable does not have time to enroll
  • or console drops error that name1 is not a function.

I know that the code written is true and correct, and independently from each other Violetta it correctly. Actually toster'and besides, I already googled and understood that it is necessary or module to sign the promises (neodnokratno what I was trying to do, but I got a little work), or used ie ES7 async/await that quite simple and fun, but for some reason this kind of code app.js did not stop the implementation where necessary:
app.all('/log', async function (req, res, next) { //this we Express
 let x = await name1(req.body.Counterparty)

The sequence of actions, as it should be:
  1. Get POST data through form
  2. Functions from a module takes the value from a field as a parameter and sends http request
  3. The result comes back (and/or written to a variable)
  4. All data is written to the database

And it turns out that step 4 happens before step 3
June 7th 19 at 15:04
2 answers
June 7th 19 at 15:06
Something like this
module.exports = async function name1(params) {
 try {
 const response = await request(params);
 let charset = [];
 charset.name = response.data;
 const charsetResult = await someFunc(charset);
 if (charsetResult.value === undefined) {
 charsetResult.value = 'PR10';
 } else {
 charsetResult.guild = charset.guild.name;
}
 return charsetResult;
 } catch (error) {
console.log(error);
};
}
now I will check the previous version, I think I took care of him (at least the data is returned, but do not pass the validation in the DB), but in the version with async/await I like more - norris_Dibbert commented on June 7th 19 at 15:09
June 7th 19 at 15:08
Like all just.
function name1 (parametrizable) {
return request (options) /*****/ 
}

...
let x = name1(req.body.Counterparty)
 .then(() => trade_log.create({/***/}));
believe that just because I tried
let x = name1(req.body.Counterparty)
 .then( return x )

I'll check this option.
And while you are checking, where there is "fresh" (not talking about past standards promises) the ES7 async/await and something like that? Of course there is google and English (which does not stop me) but without concrete examples of c github (which is not always enough) relevantnoj almost no information. - norris_Dibbert commented on June 7th 19 at 15:11
, async/await is essentially the same promises and for the coder only syntactic sugar. You need to perform the function and after the testing has to go on. Actually the option You suggested - will work for sure. - norris_Dibbert commented on June 7th 19 at 15:14
It turns out, but not quite. Cannot read property 'then' of undefined (i.e., data not written in database)
And as if in app.js code to copy:
let x = identifier(req.body.Counterparty).then(() => trade_log.create({
 Flag: req.body.Flag
 Instrument: req.body.Instrument,
 Venue: req.body.Venue
 Price: req.body.Price,
 Currency: req.body.Currency,
 Quantity: req.body.Quantity
 Counterparty: req.body.Counterparty,
 Identifier: x
 Commentary: req.body.Commentary,
 },function (err, res) {
 if (err) return console.log (x) + handleError(err);
console.log(res);
 }));

The funny thing is that in console.log(x) in the block if (err) the value of the variable is displayed is needed, but in the DB for some reason the data are not written. Is it possible to do somehow without the extra assignment to x? (I made it to understand when the data have time to enroll and when not) like:
trade_log.create({
 Flag: req.body.Flag
 Instrument: req.body.Instrument,
 Venue: req.body.Venue
 Price: req.body.Price,
 Currency: req.body.Currency,
 Quantity: req.body.Quantity
 Counterparty: req.body.Counterparty,
 Identifier: identifier(req.body.Counterparty).then(); //i.e. just a function?
 Commentary: req.body.Commentary,
 },function (err, res) {
 if (err) return console.log (x) + handleError(err);
console.log(res);
 }));
- Gonzalo.Hane39 commented on June 7th 19 at 15:17
, trade_log.create - returns a promise, remove the callback and use promises - norris_Dibbert commented on June 7th 19 at 15:20
To tell you the truth, removed the callback, but have no idea how all this rewrite, that would be over worked.

identifier(req.body.Counterparty)
 .then(() => trade_log.create({
 Flag: req.body.Flag
 Instrument: req.body.Instrument,
 Venue: req.body.Venue
 Price: req.body.Price,
 Currency: req.body.Currency,
 Quantity: req.body.Quantity
 Counterparty: req.body.Counterparty,
 Identifier: , //necessary to substitute the result in .this?
 Commentary: req.body.Commentary,
}
 .catch(function (error) {
console.log('error')
 });
- Gonzalo.Hane39 commented on June 7th 19 at 15:23
Well You have to .then after trade_log.create get the result that You console.log removed to the callback. - norris_Dibbert commented on June 7th 19 at 15:26

Find more questions by tags MongoDBAsynchronous programmingMongooseNode.jsJavaScript