Pranav
Pranav

Reputation: 673

prompt using dialogs return undefined

In my electron app, I am using dialogs npm package to do prompts. I made a basic function for prompt replacement :

function prompt(text){
    let returnval
    dialogs.prompt(text , val => {
        returnval = val
    })
    return returnval
}

And then I tested that function :

let pro = prompt("Your name");
console.log(pro); //returns undefined

But in the console, it returns undefined even after the prompt is completed. Please help me in this. Answers appreciated.

Upvotes: 0

Views: 132

Answers (1)

Dmitry Reutov
Dmitry Reutov

Reputation: 3032

According description dialog.prompt is non blocking function so the function which you pass in it

val => { returnval = val}

works only when function prompt is already returned its value

So better return the promise

function prompt(text){
    return new Promise((resolve, reject) => {
      dialogs.prompt(text , val => {resolve(val)})
    })
}

then you can use it like this

prompt("Hi, there").then(answer => console.log(answer))

Upvotes: 1

Related Questions