0

I have a pipe that returns an observable of type Observable , and I want to call this pipe from my typescript file.

**Pipe.ts**
export class Pipe {
    transform(sometihng$: Observable<SomeType>, key: string): **Observable<string>** {
        return sometihng$.pipe(
            map((sometihngs: SomeType) => sometihngs[this.service.getValue(key.toString())])
        );
    }
}

I want to call this pipe from my ts file by creating a method that returns a string :

    functionToGetValue(): String {

   // returns StringFromObservable; 

}

I don't know how the body of the function should be defined , I know how to return an observable by using the pipe , but I don't know how to return a string after subscribing to the observable.

M.Maria
  • 111
  • 4
  • 19
  • Can't you subscribe the value? – Chellappan வ Nov 18 '19 at 15:53
  • I can , but I want the function to return a string , not inside the subscription – M.Maria Nov 18 '19 at 15:55
  • There is no way to do this cause an observable is an async operation similar to Promises. That's why u can't to return a simple string from ur functionToGetValue() – Vladlen Volkov Nov 18 '19 at 16:00
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/1260204) – Igor Nov 18 '19 at 16:46
  • `"I can , but I want the function to return a string , not inside the subscription"` → You can't and shouldn't. You need to change your mindset on how you write code when it comes to asynchronous operations. Once you do that writing code becomes easier. Read through the suggested duplicates. If you still have a question [update it your question](https://stackoverflow.com/posts/58918375/edit) with how you want to use the resulting value and we can then better try to help you. – Igor Nov 18 '19 at 16:59
  • A `subscribe` can return several values over time. How would you handle it without a callback? – Christian Vincenzo Traina Nov 19 '19 at 10:25

0 Answers0