EDIT:
Disclaimer: I don't know much about Nest specifically, so this answer is from a purely vanilla JS perspective, different libraries have different built in abilities. What follows is an explanation of different ways to handle asynchronous requests and observables in javascript. I also highly recommend reading up on asynchronous javascript, observables, and promises as it will make your time in javascript far more pleasant.
Web requests in javascript happen asynchronously, meaning that they execute more or less in parallel with the rest of your synchronous code. You can imagine it like a separate thread, although it is not. This means that code that relies on the value from this web request must stall until the request is complete. From my original post below, the simplest option in your case is probably option 3. The code to use it might look a bit like this:
/**
* A method in your rest controller that relies on the getSomething()
* method as implemented in option 2 below
*/
async showRemoteData() {
const remoteData = await appService.getSomething();
// replace console.log with whatever method you use to return data to the client
console.log(remoteData);
}
Original Answer
You cannot retrieve a value from an observable in a synchronous way. You have to subscribe to it and do something once the value has been returned, or convert it to a promise and return the promise. Your options are these:
// option 1 change getSomething to doSomething, and do everything in that method
doSomething(): Array<Object> {
let resp = this.httpService.get(this.DATA_URL);
resp.subscribe((value) => { // do something })
}
// option 2 return the observable and subscribe to it outside of that method
getSomething(): Array<Object> {
return this.httpService.get(this.DATA_URL);
}
// outside of the AppService you can use it like this
appService.getSomething().subscribe((value) => {// do something})
// option 3 convert the observable to a promise and return it
getSomething(): Array<Object> {
return this.httpService.get(this.DATA_URL).toPromise();
}
// outside of the AppService you can use it like this
let value = await appService.getSomething();
console.log(value);
Of the options, option 3 allows you to use async and await which is not synchronous but allows you treat the rest of your code in the async method as though it is, so that might be closest to what you want. I personally think option 2 is your best option though as you keep all the functionality of observables, including the whole sweet of operators available to you. Embrace asynchronous code in javascript, it is the best and often times only solution to many problems.