Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!