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

159
Views
Obtener un objeto en lugar de un valor booleano

Tengo una función asíncrona que verifica el estado de los permisos en un dispositivo. Esta es la función:

 notificationsAllowed = async () => { const allowed = await requestNotifications(['alert', 'sound']).then((res) => { if (res.status == "granted") { return true; } return false; }); return allowed; }

allowed es boolean , pero cuando trato de usar esta función (por ejemplo, para establecer un interruptor en verdadero o falso) obtengo un objeto por alguna razón. Así es como trato de usarlo:

 const allowed = NotificationsManager.getInstance().notificationsAllowed().then((res) => { return res; }); const [isEnabled, setIsEnabled] = useState(allowed);

Parece que permitido es del tipo Promise<boolean> pero no puedo usar async await porque está dentro de un componente funcional.

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

0

Deberías usar useEffect para algo como esto.

 const [isEnabled, setIsEnabled] = useState(false); useEffect(() => { NotificationsManager.getInstance().notificationsAllowed().then((res) => { setIsEnabled(res); }); }, []);
about 4 years ago · Juan Pablo Isaza Report

0

Así no es como funcionan las funciones async o await el trabajo. Normalmente, con Promises (como requestNotifications ), lo ejecuta, hace su trabajo en segundo plano y luego ejecuta la función .then() cuando termina (en algún momento en el futuro).

await hace lo que dice y espera a que Promise se resuelva/finalice antes de continuar. No necesita .then() aquí, ya que está esperando que termine de una manera diferente.

 notificationsAllowed = async () => { const allowed = await requestNotifications(['alert', 'sound']); return allowed.status === "granted" };

Pero ahora notificationsAllowed permitidas son una función async . Por lo tanto, debe usar .then() que lo ejecuta en segundo plano o usar await para esperar a que se completen notificationsAllowed Permitidas.

 const allowed = await NotificationsManager.getInstance().notificationsAllowed(); const [isEnabled, setIsEnabled] = useState(allowed);

O necesita usar una devolución de llamada, que se ejecutará en algún momento en el futuro y no le permitirá devolver un valor:

 NotificationsManager.getInstance().notificationsAllowed().then(allowed => { const [isEnabled, setIsEnabled] = useState(allowed); });
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!