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

69
Views
I have a problem with a promise inside a function and a forEach method

I have these water names and I want that putting an input with a water name, the promise will return me a result. The problem is that it doesn't return anything, how can I fix it?

const water = [
  { nome: "Sant'anna", inStore: true },
  { nome: "Levissima", inStore: false },
  { nome: "Lete", inStore: true },
];

let inStoreTrue = [];
let inStoreFalse = [];
water.forEach(({ nome, inStore }) => {
  if (inStore === true) {
    inStoreTrue.push({ nome });
  } else {
    inStoreFalse.push({ nome });
  }
});

function checkWater(enter) {
  return new Promise((resolve, reject) => {
    if (enter === inStoreTrue) {
      resolve("la tua acqua è disponibile");
    } else if (enter === inStoreFalse) {
      reject("La tua acqua non è disponibile");
    }
  });
}

let prom = checkWater("Sant'anna");
prom
  .then((risultato) => {
    console.log(risultato);
  })
  .catch((err) => {
    console.log(err);
  });

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

0

The issue is with checking enter === inStoreTrue and enter === inStoreFalse

inStoreTrue and inStoreFalse are arrays. So you cannot simply equate a string with an array. You have to check whether the string is there in the array with some array methods. I used Array.some for that.

const water = [
  { nome: "Sant'anna", inStore: true },
  { nome: "Levissima", inStore: false },
  { nome: "Lete", inStore: true },
];

let inStoreTrue = [];
let inStoreFalse = [];
water.forEach(({ nome, inStore }) => {
  if (inStore === true) {
    inStoreTrue.push({ nome });
  } else {
    inStoreFalse.push({ nome });
  }
});

function checkWater(enter) {
  return new Promise((resolve, reject) => {
    if (inStoreTrue.some((item) => item.nome === enter)) {
      resolve("la tua acqua è disponibile");
    } else if (inStoreFalse.some((item) => item.nome === enter)) {
      reject("La tua acqua non è disponibile");
    }
  });
}

let prom = checkWater("Sant'anna");
prom
  .then((risultato) => {
    console.log(risultato);
  })
  .catch((err) => {
    console.log(err);
  });

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!