T
T
teodor7teodor72021-06-13 22:29:20
Express.js
teodor7teodor7, 2021-06-13 22:29:20

How to return the result of an async function from require?

I need to get the result, an object with keys, which I initialize once. How can I get the result and not the callback function?

const channels = require('../boot/channels')
module.exports = () => {
console.log(channels)
}

const Mongoose = require('mongoose');
const Location = require('../models/location');
module.exports = async () =>{
    try {
        let locs = await Location.find({});
            let channels = {
             }
            for (loc of locs) {
                channels[loc.id] = 0;
            } 
        return channels
    } catch (err) {
        console.error(err);
    }
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question