Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
Wait for a variable to be read before removing whitespace?

I'm attempting to remove spaces from a string in typescript, and one of the variables throws a big error when I refresh the page or load it fresh from the url, not when I load it from another page on my site. art.title is undefined, so I need to wait for the page to load before running the replace code.

I tried this code to define the variable I want only when it's ready, but I can't use that variable if it's defined within the conditional.

Here's the code I tried for defining the titleString variable I want:

function titleNoWhiteSpace() {
      if (typeof art.title != 'undefined') {
        const tempTitle = art.title.replace(/\s+/g, '');
        return tempTitle;
      } else {
        setTimeout(titleNoWhiteSpace, 300);
      }
    }

   const titleString = titleNoWhiteSpace();

But when I try to use titleString later, I get the following error because the variable isn't defined yet!

"Argument of type '{ titleString: string | undefined; }' is not assignable to parameter of type 'string'"

Here's the error that the code throws if I just try to replace whitespace in art.title directly:

"Uncaught TypeError: Cannot read properties of undefined (reading 'replace')"

Art types are declared here:

export interface Art {
  uri: string | undefined;
  mint: string | undefined;
  link: string;
  title: string;
  artist: string;
  seller_fee_basis_points?: number;
  creators?: Artist[];
  type: ArtType;
  edition?: number;
  supply?: number;
  maxSupply?: number;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The function titleNoWhiteSpace as you've written it doesn't wait for the title to become defined the way I think you're hoping it waits.

The function always returns a result immediately (as do all JavaScript functions), either returning the de-spaced title, or undefined. At some point the de-spaced title will be generated, but the result will just be a fleeting un-stored value happening after you've gotten the undefined value you don't want.

The only way to get the kind of wait-until-the-title-is-defined effect you want it to use a Promise or a callback function. I can't be more specific without seeing the context where the title text is ultimately supposed to be used.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!