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 theconsole.log
out of function, therefore before the execution ofthen
.– Wallace Maxters
@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.
– joao paulo santos almeida
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.– Wallace Maxters
@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.
– joao paulo santos almeida
I’ll publish as an answer then
– Wallace Maxters
@Wallacemaxters Thanks man, also published my solution.
– joao paulo santos almeida