Как заставить функцию .create() ждать заполнения таблицы перед ее возвратом.
Поскольку data возвращает undefined
const Construct = require('../models/constructModel')
const TemplateConstruct = require('../models/constructTemplateModel')
exports.create = async function () {
TemplateConstruct.find().then(function (constructs) {
let table = []
constructs.forEach((construct) => {
let newconstruct = new Construct()
newconstruct.number = construct.number
newconstruct.name = construct.name
newconstruct.basePrice = construct.basePrice
newconstruct.baseMicrowave = construct.baseMicrowave
newconstruct.atomGain = construct.atomGain
newconstruct.save().then(table.push(newconstruct))
})
console.info(table)
return table
})
// return [ 'test' ]
}
обойти это:
constructFactory.create().then(function (data) {
console.info(data)
})



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вместо того, чтобы связывать обещание через .then(), вы можете await:
const Construct = require('../models/constructModel');
const TemplateConstruct = require('../models/constructTemplateModel');
exports.create = async function () {
const constructs = await TemplateConstruct.find();
let table = [];
for (const construct of constructs) {
let newconstruct = new Construct();
newconstruct.number = construct.number;
newconstruct.name = construct.name;
newconstruct.basePrice = construct.basePrice;
newconstruct.baseMicrowave = construct.baseMicrowave;
newconstruct.atomGain = construct.atomGain;
await newconstruct.save();
table.push(newconstruct);
}
console.info(table);
return table;
};
Спасибо, это решило мою проблему. Теперь я немного лучше понимаю, как работает async!
returnобещание.