I hope you can help me, because I have some problems with the sequence of my functions (Firebase)
Note that I am a newbie (started development 3 weeks ago) and sorry for my bad english …
As you can see on the code below, I try to use async function with .then method. My function calls DB on Firebase, so some functions take longer to complete tasks.
I don’t understand why my function: SearchOrg() is finished after my function : Invitation() … while I specify to continue the code in .then …
I passed long time to try other methods as : await or Promise with function(resolve, reject) … but nothing succeeded.
read this post outlining three types of asynchronous functions, and decide which each of yours is;
pick a naming convention and follow it - here’s Google’s, for example;
declare proper types (no any) for every variable, parameter, and function return type;
never nest then or subscribe - if you feel the need to, break the task into a separate smaller function.
This will be frustrating, but following these rules will help your build tools guide you towards how to structure code clearly and avoid lots of common bugs. It will get more intuitive, eventually.
Ok, I just read about the use of Promise and function return (at first line). But if i don’t use async/await or nest .then , how i may be able to manage my tasks one after another.
One more rule: no “new Promise” in your code. There’s virtually never any need for it in app code, and it interferes with allowing types to shine through properly (and silently eats errors the way you’re using it, which is bad).
I discussed before how it’s important to avoid the crutch of any, especially when you’re starting out, because it inhibits your toolchain’s ability to help you diagnose problems. So declare an interface for all business-level objects. My minimal French is going to say searchOrg wants to return a list of Friends:
A. What is the original source providing? There are methods to convert Promises to Observables and vice-versa, but I generally attempt to avoid them, preferring to work with whatever I am given, thinking that that approach shines focus on what the app code is doing, as opposed to how it is doing it.
B. Is this a relationship or a one-night stand? Promises are specifically designed for a single operation, like “let me know when the Ionic-Native subsystem is ready for interaction”. Observables can do that as well, but really shine when the subscription is longer-lived, potentially providing a series of emissions.