Ensuring A Variable is Initialized by Promises Before Executing Further Code

Viewed 20
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?

0 Answers
Related