Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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:

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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

First you must return a value from your async function called getprefix. Secondly you must console.log the result of the promise returned by getprefix function instead of the promise itself :

const getprefix = async (id) => {
  const guildConfigs = await GuildSchema.findOne({GuildID: id});
  
  if (!guildConfigs || !guildConfigs.Prefix) {
    return DEFAULT;
  }

  return guildConfigs.Prefix;
};

getprefix(message.guild.id).then(prefix => console.log(prefix));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda