How do I return function after getting the value in http call?

Subham Sarkar

I have something like this

getFieldsRemote(){

  let fields: FormfieldBase<any>[] = [];
   this.http.get('http://localhost:8080/api/getFields').subscribe(
    data =>{
       // code to push objects from data to fields variable declared above
           }

    }

  )
    return fields;
}

Now the problem I'm facing is that whenever I call this getFieldsRemote() method from my other components I get a blank array. I understand this is happening because subscribe method is asynchronous therefore it is working in the background and the control reaches the return statement before code section inside subscribe is executed. However I need this function to somehow return the data when the subscribe method has completed. Subscribing to the data in the component side is not an option. Please tell me what's the solution here.

chrismclarke

If you don't want to have to worry about subscriptions you can simply turn into a promise and use async/await

async getFieldsRemote(){

  let fields: FormfieldBase<any>[] = [];
  const data = await this.http.get('http://localhost:8080/api/getFields').toPromise()
  // do what you want with the data
  return fields
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

How do I print return value of a function?

From Dev

Swift: How do I return a value within an asynchronous urlsession function?

From Dev

How do I return/yield a value from a recursive function in Elixir?

From Dev

how do i get the return value in json after inserting in sql?

From Dev

How do I properly call a non-return-value Swift function when it also has an overloading version that has returned value?

From Dev

No longer getting the return value of a function after decoration

From Dev

C++: How do I set precision for the return value of a function?

From Dev

How do I use a var as the return value in a mocked Jest function?

From Dev

How do I call a function or a method on a select list if the value changes?

From Dev

How do I return a value after promise is resolved Angular Typescript?

From Dev

How do I return a failure value from a bash function?

From Dev

How do I call a function after a redirect or refresh?

From Dev

How do I return by value for an "auto" return type function

From Dev

How do I return value from this function

From Dev

How do I configure or call an HTTP service in Orbeon using HTTP params and parse a JSON return value?

From Dev

how to return the data after getting response in http api call in angular 4?

From Dev

How can i continue the loop after getting value from function?

From Dev

How do I return a value from a promise to my calling function?

From Dev

How do I set the return value of a closure function to a variable?

From Dev

How do I use the input function to call the value of a variable?

From Dev

How do I make my function return 200 only after asynchronous call has finished?

From Dev

How do I call a function/return a value in VBA?

From Dev

How do I index a function call that return a array (MatLab)?

From Dev

How do I return the name of an argument in my function as a column value?

From Dev

How do I call a function that is a property value? (vue)

From Dev

How do I properly call a function and return an updated dataframe?

From Dev

I am using a function to call another function. When return the value, I am getting: 'NoneType' object is not subscriptable

From Dev

How do I pass a return value to another function and assign the return value to a variable within that function?

From Dev

How do I return a value from a function triggered by Timer

Related Related

  1. 1

    How do I print return value of a function?

  2. 2

    Swift: How do I return a value within an asynchronous urlsession function?

  3. 3

    How do I return/yield a value from a recursive function in Elixir?

  4. 4

    how do i get the return value in json after inserting in sql?

  5. 5

    How do I properly call a non-return-value Swift function when it also has an overloading version that has returned value?

  6. 6

    No longer getting the return value of a function after decoration

  7. 7

    C++: How do I set precision for the return value of a function?

  8. 8

    How do I use a var as the return value in a mocked Jest function?

  9. 9

    How do I call a function or a method on a select list if the value changes?

  10. 10

    How do I return a value after promise is resolved Angular Typescript?

  11. 11

    How do I return a failure value from a bash function?

  12. 12

    How do I call a function after a redirect or refresh?

  13. 13

    How do I return by value for an "auto" return type function

  14. 14

    How do I return value from this function

  15. 15

    How do I configure or call an HTTP service in Orbeon using HTTP params and parse a JSON return value?

  16. 16

    how to return the data after getting response in http api call in angular 4?

  17. 17

    How can i continue the loop after getting value from function?

  18. 18

    How do I return a value from a promise to my calling function?

  19. 19

    How do I set the return value of a closure function to a variable?

  20. 20

    How do I use the input function to call the value of a variable?

  21. 21

    How do I make my function return 200 only after asynchronous call has finished?

  22. 22

    How do I call a function/return a value in VBA?

  23. 23

    How do I index a function call that return a array (MatLab)?

  24. 24

    How do I return the name of an argument in my function as a column value?

  25. 25

    How do I call a function that is a property value? (vue)

  26. 26

    How do I properly call a function and return an updated dataframe?

  27. 27

    I am using a function to call another function. When return the value, I am getting: 'NoneType' object is not subscriptable

  28. 28

    How do I pass a return value to another function and assign the return value to a variable within that function?

  29. 29

    How do I return a value from a function triggered by Timer

HotTag

Archive