Is there a way to access a value from one observer within another observer?

How can I modify this line to return a value in the subscribe function instead of an observable?

let test = undefined;
of(test, this.myService.whatever(var1, var2))
    .pipe(
        first(n=>!!n) 
    ).subscribe(result=>console.log(result)); // returns Observable, should return value.

The myservice.whatever method returns an observable. I want it to resolve inside the pipe. Is there a way to achieve something like this:

let test = undefined;
of(test, this.myService.whatever(var1, var2))
    .pipe(
        first(n=>!!n),
        getValueFromObserver() // if it's an observer get the value
    ).subscribe(result=>console.log(result)); 

Is this feasible? I am attempting to verify within the observer's sequence if a local variable holds a value, and if not, fetch the value from the backend. This is necessary as the actual code is quite intricate, and the result of this observable will be combined with other observers using forkJoin.

Answer №1

While I haven't had the chance to fully test it yet, in theory you can use instanceof for checking.

let test = undefined;
of(test, this.myService.whatever(var1, var2))
    .pipe(
        first(n => !!n),
        switchMap(value => value instanceof Observable ? value : of(value))
    ).subscribe(result => console.log(result)); 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to create universal React component wrappers?

I am working with a set of functional components that share a common set of properties, for example: const A = ({ x, y, z }) = {...} const B = ({ x, y, z }) = {...} For these components, I have predefined configurations: const styles { A: { ty ...

Utilizing React Hooks efficiently with JSDoc annotations and destructuring arrays

Seeking guidance on how to properly JSDoc a React Typescript component that uses hooks, specifically when working with declared destructured arrays. Despite my efforts, I have not been able to find a solution that allows JSDoc to work seamlessly with destr ...

Executing a function in a child component when a change event occurs on a select element within the parent component

Is it possible to call a function in a child component once the (change) event is triggered on a select element in the parent component? ...

Enhancing external TypeScript modules

Currently, I am delving into the realm of enhancing external modules in TypeScript. I am diligently studying the official documentation and the DefinitelyTyped guides, poring over examples, and so forth. At this point, my goal is to incorporate custom prop ...

Provide a boolean value of true or false to indicate whether all delete operations were successfully completed

Currently, I am using Sequelize, GraphQL, and Typescript for my coding. Within my database, I have two tables named RecordInformation and OtherDescription. The RecordInformation table contains a foreign key called "OtherID" which references the OtherDescri ...

What is the best way to retrieve HTML content using an Angular method?

Okay, so the title might not be the greatest...but I couldn't think of anything better: I want to emphasize search keywords in the result list...that's why I'm having trouble with this problem. CSS: .highlightText{ font-weight: bold; } In ...

When trying to call a function within a component in Angular2, it's important to remember that self.context

When I try to call a function upon instantiating an angular2 component, an error is triggered: TypeError: self.context.dummyFunc is not a function To illustrate the issue, consider the following example. Say we have this component: import { Componen ...

Deactivate user session in LoopBack 4 API

Can anyone provide a clear example of an API endpoint for logging out that allows for deleting the token stored during login instead of relying on the web browser? It seems there is no documentation available on how LoopBack generates a default user when ...

Guide on creating a zodiac validator that specifically handles properties with inferred types of number or undefined

There are some predefined definitions for an API (with types generated using protocol buffers). I prefer not to modify these. One of the types, which we'll refer to as SomeInterfaceOutOfMyControl, includes a property that is a union type of undefined ...

Navigating in Angular 2.0.0-rc1 with routes and setting a default route using use

Is it true that "useAsDefault" has been removed in angular2.0.0-rc1? Any suggestions for a workaround? I noticed in the Angular documentation they are using the OnInit method... Do subroutes still function with the /... notation? Thanks ...

Securing Single Page Applications

Have you ever wondered how SPA ensure the security of their sites? With all the embedded scripts, it seems like anyone could access and analyze the code. Do you have any thoughts on this? Additionally, when connecting to other web services that require sp ...

What is the reasoning behind an empty input value being considered as true?

I am facing an issue with the following code that is supposed to execute oninput if the input matches the answer. However, when dealing with a multiplication problem that equals 0, deleting the answer from the previous calculation (leaving the input empt ...

Tips for addressing the issue - error TS2451: Unable to redefine block-specific variable 'total'

I am currently working on setting up the Jest testing framework and running a basic test (using an example from jestjs.io). Here is the content of the sum.ts file: function sum(a: any, b: any):any { return a + b; } module.exports = sum; And here is the ...

Standard layout for a project with equally important server and client components

We are in the process of developing an open-source library that will consist of a server-side component written in C# for Web API, meta-data extraction, DB operations, etc., and a client-side component written in TypeScript for UI development. Typically, ...

Sending file URL to Angular component

Hey there, I am looking to relocate an existing link within my Angular 4 project. For instance, the link I want to move is "". I have attempted to do so in my router configurations: path: 'test', component: TestComponent, children: ...

Tackling the white-source security problem in npm libraries

A security advisory from White-source has identified high vulnerability issues with certain libraries used in your repository, specifically with yargs-parser: 1. build-angular-0.13.8.tgz (Root Library) node-sass-4.11.0.tgz sass-graph-2.2 ...

CORS policy blocked Axios React GET request

I am attempting to utilize Axios for fetching an API from in order to receive a response. I am currently facing difficulties in resolving the encountered issue. My Axios call is simple and located in the following file: api/index.js import axios from &q ...

What methods are available to change one JSON format into another?

I am receiving JSON data from a Laravel API in the following format: [ { "id":48, "parentid":0, "title":"Item 1", "child_content":[ { "id":49, "parentid":48, "title":"Itema 1 ...

Leveraging the power of RXJS and typescript for executing work conditionally between Server and Client Code

I am in need of a function that will assess various conditions to determine if an object is eligible for editing. These conditions may exist on both the server and client sides. Certain conditions should halt further validation and return a value. ...

Error message: "Unable to emit an array with Angular

Within the DateRangeComponent, I am attempting to trigger an array emission to another component (ViewerComponent) upon button click using the EventEmitter and Output decorator. The DateRangeComponent contains a getData() method which utilizes the EventEm ...