I'm currently facing a challenge with implementing a synchronous user prompt in my electron app. Specifically, I have an object that contains a series of commands and template variables.
My objective is to substitute all unknown template variables with user input... synchronously. This means that the commands should only be executed after all variables have been replaced by user input.
Could you assist me with this issue?
Below is how I am initiating a sync user prompt (bootstrap model with a form) on my end (this test is functional and I receive the result
synchronously once the user enters something in the prompt):
async function test(gui) {
const result = await gui.syncPrompt('User query')
console.log('result:', result)
}
test(this.gui)
My dilemma lies in understanding the various async/await statements and how to integrate them into my standard replacement procedure. Here's my current progress:
const obj = {
cmds: [
'port {port}',
'template1 {temp1} and template2 {temp2}',
'template2 {temp2} and template1 {temp1}'
]
}
const templatePrompt = async () => {
const map = {}
await obj.cmds.forEach(async (element, index, array) => {
const patt = /{.*?}/gmi
patt.lastIndex = 0
if (patt.test(element)) {
await obj.cmds[index].match(patt).map(async (value) => {
let userInput = map[value]
if (!userInput) {
// Create Prompt here.
// userInput = Math.random() * 10
userInput = await this.gui.syncPrompt('User question:')
}
map[value] = userInput
return true
})
await Object.keys(map).map(async (key) => {
obj.cmds[index] = obj.cmds[index].replace(key, map[key])
return true
})
}
})
}
await templatePrompt()
console.log(obj)
I neglected to mention that my main challenge is... the templatePrompt() function runs and the first prompt appears. However, before the user inputs any data, the entire process is already completed without replacing the template variables. :( My aim is to pause at each prompt until user input is provided.