Variable loses value after leaving Promisse

Asked

Viewed 323 times

1

Hello, I’m having the following problem, I have a variable that should store the data coming from a promisse, so that this data can be used in other functions, But for some reason the variable keeps the data of promise only while this promise is being executed, after leaving it the variable is back to Undefined. I’m doing Angular 1 and Pouchdb.

In Pounchdb it is possible to pick up a JSON document from a database within the browser.

   //Varivel que armazenda dados
   var salvaAqui;

   db.get('mydoc').then(function (doc) {
     salvaAqui = doc;
     console.log(salvaAqui); // Retorna resultado esperado.
   }).catch(function (err) {
   console.log(err);
   });

   console.log(salvaAqui); // Retorna undefined

I just need to know how to keep the "data" that promised it gave the variable. Who can help me I thank.

  • In fact, the execution is happening at different times. Looking through your code, that’s what you mean. The variable has the value of doc, but you’re giving the console.log out of function, therefore before the execution of then.

  • @Wallacemaxters, in debugging o a promisse ta being executed before the console.log at the end, and inside then already has a log that returns the normal data.

  • It doesn’t make much sense. In fact, it usually uses promisse functions to request parallels (or asynchronous) resources. When you give that console.log at the end, without anything being returned, it is absolutely understandable: the variable has not yet had the callback value of then executed. You have to understand that in javascript, the order of the code does not mean the order in which the results will be obtained. In an ajax request for example, giving a console.log after its call would not change any value of the variable.

  • @Wallacemaxters worked now, you were really right I mosquei here at debug time. I put my functions using promisse data within another function that only and call within the promisse then, so it worked.

  • I’ll publish as an answer then

  • @Wallacemaxters Thanks man, also published my solution.

Show 1 more comment

2 answers

2


In fact, the execution is happening at different times. Looking through your code, that’s what gives to understand.

The variable does have the value of doc, but you’re giving the console.log outside the callback executed by then. Therefore, before the execution of then, no value will still be assigned to salvaAqui.

Usually, we use functions with predecessors to request parallel (or asynchronous) resources. When you give that console.log at the end, without anything being returned, it is absolutely understandable: the variable has not yet had the callback value of then executed.

You have to understand that in javascript, the order of the code does not mean the order in which the results will be obtained. In an ajax request for example, the fact of giving a console.log after your call would not change any value of the variable in the same scope in which it was declared, but only after the completion of the request.

Example:

var dados; // undefined 
$http.get(url).then(function (response) {
     dados = response.data; // [Object Object]
});

console.log(dados); // undefined

The suggestion in this case is to do the required operation with the data you need within the then.

Something like this:

db.get('mydoc').then(function(doc) {
    processarDoc(doc); // passa como argumento
}).catch(function(err) {
    console.log(err);
});

function processarDoc(doc) {

    console.log(doc); // [Object Object]
}
  • 1

    The guy knows, helped me a lot this answer, I was doing a scam in my code that even I no longer understand what was happening there.

0

promisse is executed after the rest of the code, so the value returns Undefined. my solution was

    //Varivel que armazenda dados
    var salvaAqui;

    db.get('mydoc').then(function (doc) {
    salvaAqui = doc;
    executaResto();
    }).catch(function (err) {
    console.log(err);
    });

    function executaResto(){
       console.log(salvaAqui); // Agora retorna os dados normal
    }
  • I also added an example. I preferred to call the function by passing as parameter, rather than using a variable declared outside the scope. Have a look

Browser other questions tagged

You are not signed in. Login or sign up in order to post.