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

142
Views
node chaining then no return

I am new to async function and I'm tring to chain some function but I can't figure out why I can't return the last call

var container = { Login: '', sign_up_text: '', forget_password: '', login_button: '' }
var lang = 'de-DE'
var langID = 1

async function getVars (key, lang){
    return db.localization.findOne({
        where: {
            key: key,
            lang: lang
        }
    })
}
async function getLangID (lang){
    return db.localGroups.findOne({ where: {
            i10s: lang
        },
        attributes: ['id']
    })
}
async function getLocalvars (container, lang){
    var newcontainer = container
    for (let key in container) {
        console.log(lang)
        await getVars(key, lang)
            .then(data => {
                newcontainer[data.key] = data.value;
            })
    }
    return newcontainer;
}
async function desprate() {
    getLangID(lang)
        .then(langdata => {
            getLocalvars(container, langdata.id)
                .then(data => {
                    console.log(data);
                    return data;
                })
        })
}
console.log(desprate());

Not even a promise shows up in the log ;(

Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'll give this a shot. I haven't tested this but I'll try to hit the key concepts here in the comments.

var container = {
  Login: '',
  sign_up_text: '',
  forget_password: '',
  login_button: '',
};
var lang = 'de-DE';
var langID = 1;

async function getVars(key, lang) {
  return db.localization.findOne({
    where: {
      key: key,
      lang: lang,
    },
  });
}

async function getLangID(lang) {
  return db.localGroups.findOne({
    where: {
      i10s: lang,
    },
    attributes: ['id'],
  });
}

async function getLocalvars(container, lang) {
  var newcontainer = container;
  // since you're iterating over an object you will have to await all the promises that result in the loop
  await Promise.all(
    Object.keys(container).map(async (key) => {
      const data = await getVars(key, lang);
      // since this is an async function you can just await it rather than chaining it with then
      newcontainer[data.key] = data.value;
    })
  );
  return newcontainer;
}

async function desprate() {
  // let's again take advantage of async/await here
  const langdata = await getLangID(lang);
  const data = await getLocalvars(container, langdata.id);

  console.log(data);
  return data;
}

console.log(desprate());
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!