Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Async Function returns promise

I have a function that returns the guilds prefix in Discord.JS:

getprefix.js:

const GuildSchema = require("../Database/Models/GuildConfigs");
const { DEFAULT } = require("./config");

const getprefix = async (id) => {
  const guildConfigs = await GuildSchema.findOne({
    GuildID: id,
  });

  let PREFIX = DEFAULT;

  if (guildConfigs && guildConfigs?.Prefix) {
    PREFIX = guildConfigs?.Prefix;
  }
};

module.exports = { getprefix };

I call the function in another file using this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let prefix = getprefix(message.guild.id);

prefix.then(() => {
    console.log(prefix);
});

The problem is it returns this in the console:

Promise { '!' }

Is it possible to just return the actual prefix that is inside the quotes with out the Promise?

>Solution :

Yes, but you must return the value from the async function.

getprefix.js:

const GuildSchema = require("../Database/Models/GuildConfigs");
const { DEFAULT } = require("./config");

const getprefix = async (id) => {
  const guildConfigs = await GuildSchema.findOne({
    GuildID: id,
  });

  let PREFIX = DEFAULT;

  if (guildConfigs && guildConfigs?.Prefix) {
    PREFIX = guildConfigs?.Prefix;
  }
  return PREFIX;
};

module.exports = { getprefix };

and change the call:

let prefix = getprefix(message.guild.id);

prefix.then((value) => {
    console.log(value);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading