-11

I am building an application in Cordova using Firebase for the database.

I have been using promises to get information back from the database and it works great, but once I get that back I usually have a series of functions that I need to call to update everything.

For example,

  1. update local storage

  2. call variable from local storage

  3. reload page with updated storage information

I need to create/convert my JavaScript functions to be able to be called in succession.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hefty_kat
  • 211
  • 4
  • 15
  • 6
    Can you edit the question title so it reflects the question's content? – McVenco Nov 12 '18 at 21:24
  • 3
    Rewrite your title – Dexygen Nov 12 '18 at 21:25
  • Most likely your issues stem from `updateCS()` not returning a Promise (or even if it does, you're not using it) – Paul Nov 12 '18 at 21:25
  • 1
    I don't get your explanation why you "*cant use a .then on this or place it in the .then above*". Please try it and show us that code, the approach should work. – Bergi Nov 12 '18 at 21:28
  • That isn't possible. You can't cause the `return` to wait. You'll have to instead do something else, like return false by default, disable the input, then when the work is done apply the change or don't and then re-enable the input. – Kevin B Nov 12 '18 at 21:28
  • 1
    Don't blame the language...blame the developer – charlietfl Nov 12 '18 at 21:34
  • JS is the best language ever, it [has really nice features](https://stackoverflow.com/questions/1995113/strangest-language-feature) – Jonas Wilms Nov 12 '18 at 21:43
  • https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Josh Lee Nov 12 '18 at 23:14
  • lul so much hate. @charlietfl why do you think I am here? I am asking because I know i can't figure it out. – hefty_kat Nov 14 '18 at 19:56
  • @JonasWilmsI like JS i just dont get await and promises and how to use them for my needs. I've read the docs but still don't see how to apply it. – hefty_kat Nov 14 '18 at 19:59
  • @KevinB it isn't returning a promise. – hefty_kat Nov 14 '18 at 19:59
  • @McVenco Edited title. If you have a better option i'm all ears :) – hefty_kat Nov 14 '18 at 19:59

1 Answers1

1

What you are really asking is how to implement a function after a promise is done. If so, then making the function an Async function will do the trick.

For information on async-await check out Async/await.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
YulePale
  • 6,688
  • 16
  • 46
  • 95
  • Yeah, basically I have 8 functions that get called through out my app. Some of them need to be called in succession for the other ones to work. For example: update local storage variable needs to be done updating before i call that local storage variable to populate what is on the page. then once this is all done reload the page. I read that async stuff but without a more specific example it has always been hard for me to reverse engineer. – hefty_kat Nov 14 '18 at 19:54
  • @hefty_kat try watching youtube videos, I find them easier to understand. Check out this video: https://www.youtube.com/watch?v=IGoAdn-e5II. The concept is well explained in a simple manner. – YulePale Nov 15 '18 at 04:35