Link to home
Start Free TrialLog in
Avatar of Alex Lord
Alex Lord

asked on

Getting value from another function than returning to previous function

function getNote(){
     bootbox.prompt({
        title: "Update status to closed",
        inputType: 'textarea',
        callback: function (note) {
           
                
                return note;
        }
    }); 
    
}

function setGlobalVariables(val , id){
    
    
    var getEID = val.split('-')[1];
    var getName = val.split('-')[0];
    var getRule = id.split('-')[0];
    
   getNote();
    
    alert(getNote());
    
    

Open in new window



so here my code, i am at the part where i am sending user to a dialog box to type in a note, is it possible for me to go off to this function from within setGlobalvar function get a value from getNote function and then return it to the setGlobalVar function ?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

No because the getNote() function is asynchronous.

When you are dealing with an async callback and you want to get the value from that callback you need to return a promise that the calling function can use to extract the data when the promise resolves.
[You can read more on Promises here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise]

What does this mean practically?

In our example it is not possible to determine if the bootbox.prompt() method returns anything - it is possible it returns a promise - but if it does not then you will have to create one.

function getNote()
{
  var promise = new Promise(function(resolve, reject) {
    bootbox.prompt({
      title: "Update status to closed",
      inputType: 'textarea',
      callback: function (note) {
        resolve(note);
      }
    }); 
  });
  return promise;
}

Open in new window

Now in your calling code you would do this
function setGlobalVariables(val , id)
{
  var getEID = val.split('-')[1];
  var getName = val.split('-')[0];
  var getRule = id.split('-')[0];
    
   getNote().then(function(resp) {
     alert(getNote());
   });
}

Open in new window

In the above example we create a promise in the getNote() function and return this. A promise is a construct that we can pass around our application to which we can attach a completion handler which will fire when the async function it contains completes (resolves)

In your case return note; in your callback will do nothing. An asynchronous function completes in unknown time so at the time your return statement fires - the rest of your application has finished running.

That is why we use a promise. We return the promise to the setGlobalVariables() function and attach a then() callback handler to handle the response from the getNote() callback.

Any code you want to run after the bootbox.prompt() callback fires you need to put in the then() handler as shown above.

[NB NB]
Be sure to check your bootbox.prompt() documentation to see if it does not already support a promise return - if it does then use that rather than creating your own.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.