The promise briefly returns a placeholder object before resolving with the actual response

Currently, I am working on a simple check to determine whether myAnswer contains an answer. The checking functionality is operating smoothly; however, the issue arises in the final function that aims to return the string obtained from myAnswer. Instead of directly returning the string, it displays [object Promise]. This behavior is puzzling as I expect the resolved promise to return the string instead.

I would appreciate any assistance or insights on this matter!

this.myAnswer = XapiStoreService.retrieveQuestionFillIn(this.app.getStudent(), pages[pageIndex])
                .then((answer:string) => {
                  this.myAnswer = answer;
                }, (reason:string) => {
                  this.myAnswer = '';
                });

Answer №1

You are currently

setting the value of this.myAnswer using XapiStoreService.retrieveQuestionFillIn(…).then(…);

This action creates a promise for myAnswer. However, you will only briefly see this before the promise is resolved and updates the myAnwer variable. It is likely that you actually intend to do something like

this.myAnswer = "loading";
XapiStoreService.retrieveQuestionFillIn(this.app.getStudent(), pages[pageIndex]).then((answer:string) => {
    this.myAnswer = answer;
}, (reason:string) => {
    this.myAnswer = 'error';
    console.error(reason);
});

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

Differentiating elements from two array objects in typescript: a guide

I am facing an issue in extracting the different elements from two array objects. Here is my example: array1 = [{id: 1, a: "a", b: "b"}, {id: 2, c: "c", d: "d"}, {id: 3, e: "e", f: "f"}]; array2 = ...

Blue Jay Guarantees: Construct props object on the fly and execute simultaneously

If we take a look at this example: https://github.com/petkaantonov/bluebird/blob/master/API.md#props---promise Promise.props({ pictures: getPictures(), comments: getComments(), tweets: getTweets() }).then(function(result) { console.log(re ...

An issue has occurred while attempting to differentiate '[object Object]'. Please note that only arrays and iterable objects are permitted

myComponent.component.ts ngOnInit() { this.getData.getAllData().subscribe( response => { console.log(response); this.dataArray = response; }, () => console.log('there was an error') ); } myservi ...

Enhancing User Experience through 'Remember Me' Feature in Angular App

I need assistance with adding the Remember Me functionality to a login form in an Angular application. Could someone please provide guidance on how to achieve this? Your help would be highly appreciated! Thank you in advance! Below is my login.ts file: ngO ...

Tips for sending props, state, or arguments to a TypeScript React component

Hey there, total newbie here... I'm currently working on a school project and I've hit a bit of a roadblock... I'm attempting to pass some props from one component to another, but for some reason, it's not working properly. The goal ...

Typescript may fall short in ensuring type safety for a basic reducer

I have been working on a simple reducer that uses an object to accumulate values, aiming to maximize TS inference. However, I am facing difficulties in achieving proper type safety with TypeScript. The issue arises when the empty object does not contain an ...

Is it feasible to conditionally set a function parameter as optional?

type TestType<A> = [A] extends [never] ? void : A class Singleton<T, A> { private ClassRef: (new (...args: A[]) => T) private args: TestType<A> private _instance?: T constructor(ClassRef: (new (...args: A[]) => T), ...

Intro.js is not compatible with React and Remix.run

I am currently working on implementing onboarding modals for header links using intro.js within a React environment. Below is the code snippet: import { useState, type FC } from 'react' import type { Links } from '../types' import &apo ...

Leveraging Class Types with Generics

Take a look at this example: https://www.typescriptlang.org/docs/handbook/2/generics.html#using-class-types-in-generics To make it work, I just need to call a static method before instantiation. Let's adjust the example like this: class BeeKeeper { ...

List out the decorators

One query is bothering me - I am attempting to create my own version of Injectable and I need to determine if a specific decorator exists in my Class. Is there a way to list all decorators of a class? Let's take the example below. All I want to know i ...

What is the best way to set a boolean value for a checkbox in a React project with Typescript?

Currently, I am working on a project involving a to-do list and I am facing an issue with assigning a boolean value to my checkbox. After array mapping my to-dos, the checkbox object displays 'on' when it is unchecked and a 'Synthetic Base E ...

JavaScript/Typescript is throwing an error because it is unable to access the property 'username' of an undefined object

In my project, I am attempting to compile a list of Profile objects and then extract specific elements from each object in the list. To accomplish this, I have defined a public interface named Profile, imported it into my component, and instantiated a new ...

Encountering difficulties when trying to deploy Angular 4 with TypeScript on Heroku

I have been facing an issue while trying to deploy my angular 4 app on Heroku. The deployment fails to proceed properly even though everything works fine locally. Here are the relevant files: package.json { "name": "webcli2", "version": "0.0.0", " ...

What is the best way to create two MUI accordions stacked on top of each other to each occupy 50% of the parent container, with only their contents scrolling

I am looking to create a layout with two React MUI Accordions stacked vertically in a div. Each accordion should expand independently, taking up the available space while leaving the other's label untouched. When both are expanded, they should collect ...

What is the best way to refresh the snapshots in my React/TypeScript project?

I am working on a React/TypeScript project that utilizes the Jest testing framework. Occasionally, after making changes to my code, Jest will compare it to the snapshots and generate an alert requesting me to update them. However, there are instances where ...

Learn how to create a versatile TypeScript function that combines an array parameter and values to form an object

I've created a function that combines an array of keys with an array of values to form an object. Here's how the function looks: function mergeToObject(keys: string[], values: string[]) { const object:? = {} for (let i = 0; i < keys.length ...

What could be the reason for the 'tsc' command not functioning in the Git Bash terminal but working perfectly in the command prompt?

I recently installed TypeScript globally on my machine, but I am facing an issue while trying to use it in the git bash terminal. Whenever I run tsc -v, I encounter the following error: C:\Users\itupe\AppData\Roaming\npm/node: l ...

There are two modals present on the page, however only one of them is triggered for all actions in Angular 2

I'm encountering an issue with my page where I have set up two confirmation modals - one for resetting a form and another for deleting an item. Strangely, only the reset modal is being triggered for both actions and I can't figure out why. Could ...

Using TypeScript with React: Specifying a type when calling a React component

How should a type be passed into a component call? Within this scenario, the render prop APICall is utilizing a type parameter named MobileSplashReturn. Although this example is functional, it seems to be causing issues with prettier, indicating that it m ...

Dealing with a standalone 404 error in AngularJS's $q

Currently, I have multiple chained $http requests combined with a single $http request using $q.all([promiseA, promiseB]). Everything is going smoothly as expected - data is returned and errors are being handled effectively. However, there are instances w ...