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

117
Views
Ensuring A Variable is Initialized by Promises Before Executing Further Code
function loadStrings(file){
    var strings = [];
    fetch(file).
        then(response => response.text()).
        then(data => strings = data.split("\n")).
        then(printStrings(strings));
}

function printStrings(strings){
    console.log(strings.length);
    for(string of strings){
        console.log(string);
    }
}

In this example, console.log(strings.length) prints zero. I thought attaching a then() after assigning to strings would ensure that strings was written to before I actually tried to print it out. How can I ensure that they're written to before I work with them?

about 4 years ago · Juan Pablo Isaza
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!