Component Editing: Enhancing the Dynamic Promise

My component requires a Promise as an input:

export class Component  {
    @Input() appendingFunction: Promise<PagedData<any>>
}

The function assigned to "appendingFunction" might be structured like this:

async foo(importantParameter, parameter1,..parameter n): Promise<PagedData<City>>

Before resolving appendingFunction, I need to set the value of importantParameter. While I know it will be there, I'm unsure how to define it specifically. Other parameters are not relevant in this case.

How can I achieve this?

Overall structure:

External Component 1:

export class ExternalComponent1{
   async foo(importantParameter, x):Promise<PagedData<boolean>>{
   ...
   }
}

External Component 2:

export class ExternalComponent2{
   async foo(importantParameter, x, y, z):Promise<PagedData<number>>{
   ...
   }
}

HTML markup for both components:

<inner-component [appendingFunction]="foo()"></inner-component>

Inner Component:

export class InnerComponent  {
    @Input() appendingFunction: Promise<PagedData<any>>

onClick(){
  //This is what I need
  appendingFunction.setImportantParameter();
   appendingFunction.then(() => doSomething)

}

Answer №1

Consider approaching your solution with a fresh perspective.

One approach is to develop a service called AppendingBridgeService structured like this:

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';

@Injectable({
  providedIn: 'root'
})
export class AppendingBridgeService {
  private _importantParam = new Subject<any>();
  importantParam$ = this._importantParam.asObservable();

  constructor() {}

  public next(nextParam: string) {
    this._importantParam.next(nextParam);
  }
}

In your Outside components, you can then inject and utilize this service:

export class OutsideComponent1{
  constructor(abs: AppendingBridgeService) {
    this.abs.importantParam$.subscribe(nextParam => {
      this.foo(nextParam, x).then(pagedData => {
        doSomething;
      });
    });
  } 
}

Connect the inner component to the same bridge service by injecting it as well:

export class InnerComponent  {
    constructor(abs: AppendingBridgeService) { }

    onClick(){
        this.abs.next(importantParameter);
    }
}

To link the inner component to the result of the promise in the Outside components, consider creating another bridge service that handles the reverse process by accepting a different type, such as PagedData<any>.

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

Discovering the right place to establish global data in Nuxt JS

Exploring the world of NuxtJS today, I found myself pondering the optimal method for setting and retrieving global data. For instance, how should a frequently used phone number be handled throughout a website? Would utilizing AsyncData be the most effecti ...

Asp.net isn't cooperating with Jquery Ajax Call

I've tried multiple solutions, but nothing seems to be working. I'm attempting a simple Ajax call using Jquery in Visual Studio 2019, but the code is not hitting the Ajax call. Here's the snippet of my code: <script src="Scripts/jqu ...

Iterate over the array elements in React by using Hooks on click

I am facing an issue with loading objects separately from a JSON file when a button is clicked. The problem occurs when the index goes out of bounds, resulting in a TypeError "Cannot read property 'content' of undefined" message. I have tried u ...

Display a free Admob banner within an Ionic 3 application

I have integrated Admob's banner into my Ionic 3 app following the guidelines provided in the Ionic documentation at this link. Below is the code snippet I used for displaying the banner on the homepage: import { Component } from '@angular/core ...

Error: The variable "require" cannot be located

I'm having trouble loading the node_modules onto one of my webpages. Despite having npm and node.js installed, I keep getting a ReferenceError when trying to use the require() function to initialize Firebase on my website. ReferenceError: Can' ...

Utilizing ReactJS: Expanding my knowledge of updating array object indexes dynamically when removing elements

Currently, I am in the process of creating a to-do list application to improve my skills in working with ReactJS. This is how my initial state looks like: const [listx, setlistx] = useState( [ {id: 0, flavor: 'strawberry', ...

Conceal any elements designated by a particular class and reveal them based on their corresponding ID

I have utilized jQuery's hide() function to initially hide all elements of a specific class when the page loads. My goal is to make individual elements visible again based on their unique IDs when a corresponding link is clicked. In total, there are ...

How can I easily activate a C# class from an asp.net webpage?

I have three labels in my asp.net page: One with a default name for filtering Another filled with a list of names to click on for new filter A third one with a list of items to be filtered The content of the second and third labels is loaded by C# code ...

Employing ajax.actionlink in mvc4 results in a page refresh

I am working on a view that looks like this: @model IEnumerable<DomainClasses.Class> @{ ViewBag.Title = "لیست "; } <div id="test"> <h2>لیست کلاس ها</h2> <p> @Html.ActionLink("ایجاد کلاس جدی ...

Trigger the dimming effect on the webpage when HTML5 video starts playing

After using this particular script: <script> myVid=document.getElementById("video1"); myVid.oncanplay=alert("Can start playing video"); </script> http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_av_event_canplay A notification windo ...

What are the steps to turn off the rule .MuiInputBase-inputType-121 in Material UI TextField for email input field because of a display problem?

Here is a snippet of JSX code: import TextField from '@material-ui/core/TextField/TextField'; <Col xs={6}> <TextField pattern=".{3,}" name="fullName" ...

Executing one specific test in Protractor using Selenium

How can I execute a single test in Protractor with Selenium? describe('Login page', function() { beforeEach(function() { browser.ignoreSynchronization = true; ptor = protractor.getInstance(); }); it('should contain navigation items&ap ...

Increase the Z-Index of the Header above the Material UI Modal Background

Currently, I'm in the process of developing a mobile version of a web application using Material UI. However, I am encountering some challenges when it comes to matching the designs accurately. My approach involves utilizing the MUI App-Bar in conjunc ...

Issue with Formik compatibility in Next JS 14 Application Structure

I attempted to create a basic validation form using Formik. I meticulously followed their tutorial and example, but unfortunately, the form is not functioning correctly. Despite my efforts, I have been unable to identify a solution (Please correct me if I& ...

Combating React SetState Race Conditions with the Power of Promise.all

componentDidMount() { Promise.all([OfferCreationActions.resetOffer()]).then( () => { OfferCreationActions.updateOfferCreation('viewOnly', true); OfferCreationActions.updateOfferCreation('loadingOffer', true); ...

Having trouble accessing Angular 1.5 Scope Variable within PouchDB response function?

So, take a look at this code snippet... testApp.controller(...) { $scope.results = []; $scope.hasData = true; $scope.results.push({ "name": "test" }); // This part works fine db.get('table_people').then(function(res ...

Is there a method to stop react-select (Select.Async) from erasing the search input value when it loses focus?

Situation: In my setup, I have a standard select element (categories), which dictates the options displayed in a Select.Async component from react-select. Problem: Consider this scenario: a user is searching for an option within Select.Async whil ...

Create a dynamic fbx model complete with textures and animations using three.js

I have been working on showcasing "animated 3D Models" on a webpage. These models come in the form of .obj, .mtl & .fbx files, some with textures and some without. I've managed to display .obj 3D Models on the webpage successfully (with texture and mt ...

Loading content synchronously

Could you assist me in finding a solution to synchronize the loading of pages and elements? Currently, my code looks like this: $(Element).load(File); ... other commands... window.onload = function () { Update_Elements(Class, Content); } Everything ...

Error message: "NAN encountered while attempting to extract a numerical value from

I've encountered a strange bug in my coding. I'm working on a weather forecasting website that uses geolocation to identify your city and then utilizes the wunderground API to provide the forecast. The issue arises when manually searching for a c ...