Additional confusion about File Chooser (launchAsync)

This is a follow-up to this post: [link]

I’ve successfully gotten my code to the point where I can save a file, but I’m having a lot of trouble opening and reading existing ones. The asynchronous property of the file chooser is causing my code to continue executing, when subsequent code relies on data I would be extracting from a given chosen file.

My problems are occurring in this function:

void NodeMCUPatternProgrammer::loadPatternFromFile()
{
    clearSlots();

    // I need the data read from a JSON file here:
    nlohmann::json jsonData = _fileChooserJSONHandler_Ref->readDataFromJSONFile();
    // But execution just keeps going to here, despite no file having yet been selected.
    _patternMultiplier = jsonData["multiplier"]; // Exception thrown here due to no JSON data present
    
    nlohmann::json pattern = jsonData["pattern"];
    for (int i = 0; i < pattern.size(); i++)
    {
        int r = pattern[i]["r"];
        int g = pattern[i]["g"];
        int b = pattern[i]["b"];
        TIP_RGB tempRGB(r, g, b);
        buildNodeFromRGB(tempRGB);
    }
}

The readDataFromJSONFile() function is below: It’s not in a finished state right now, but this doesn’t change the issue of the file chooser prompt not actually appearing until after I dismiss the exception caused by my attempting to read an, at the time, empty JSON structure.

nlohmann::json FileChooserJSONHandler::readDataFromJSONFile()
{
    _fileChooser.reset(new juce::FileChooser("Choose a file to open...", juce::File::getCurrentWorkingDirectory(),
        "*", true));
    nlohmann::json fileContentsAsJSON;

    _fileChooser->launchAsync(juce::FileBrowserComponent::openMode
        | juce::FileBrowserComponent::canSelectFiles,
        [fileContentsAsJSON](const juce::FileChooser& chooser)
    {
        juce::String chosen;
        auto results = chooser.getURLResults();

        for (auto result : results)
            chosen << (result.isLocalFile() ? result.getLocalFile().getFullPathName()
                : result.toString(false)) << "\n";

        juce::AlertWindow::showAsync(juce::MessageBoxOptions()
            .withIconType(juce::MessageBoxIconType::InfoIcon)
            .withTitle("File Chooser...")
            .withMessage("You picked: " + chosen)
            .withButton("OK"),
            nullptr);

        std::ifstream ifs(chosen.toStdString());
        nlohmann::json temp = nlohmann::json::parse(ifs);
    });
    return fileContentsAsJSON;
}

This, and my previous post accentuate what I think is a fairly large issue with the fileChooser class: which is that most of the documentation around it still refers to functions that can’t even be accessed without changing JUCE_MODAL_LOOPS_PERMITTED to true. I’d like to do this the way JUCE currently intends and not use features that seem to be deprecated, but the documentation about how to use the launchAsync function is sparse at best. Any suggestions on how to advance here?

Add an std:: function callback parameter to the read json function, pass a lambda with the code you want to execute after file selection, and call that in the lambda you already have in the launchAsync call.