hello please help me I have a problem with this function I would like him to return the result of the search in the database my results are always wrong.
test(value) {
var res;
this.storage.get('age').then((currencies) => {
this.getCurrentCurrency().then((val) => {
res = value * currencies[val]['rate'];
});
});
return res;
}
Thanks. I want to return res
Hi, @durvilchat
What is the result could you print here:
thanks
Hi @addwebsolution
res = value * currencies[val][‘rate’];
means what is res ?
I mean what is wrong result ?
Hi @durvilchat
try below code :
test(value) {
var res;
this.storage.get('age').then((currencies) => {
this.getCurrentCurrency().then((val) => {
res = value * currencies[val]['rate'];
return res;
});
});
}
res because he returned before the end of this operation
hello please help me I have a problem with this function I would like him to return the result of the search in the database my results are always wrong.
test(value) {
var res;
this.storage.get('age').then((currencies) => {
this.getCurrentCurrency().then((val) => {
res = value * currencies[val]['rate'];
});
});
return res;
}
Thanks. I want to return res
Hello,
have you tried @addwebsolution code?
Take a look where return is. In your case return is in syncronous part, he’s case is return in asyncronous part.
And nothing has changed?
Best regards, anna-liebt.
@addwebsolution please are you have a solution ??
Let me know you should try my above code
this code i was tell you…
@addwebsolution this code return undefined value
here the console.log(test(250)) return undefined value
try below code :
test(value): :Promise<any> {
return new Promise(resolve => {
this.storage.get('age').then((currencies) => {
this.getCurrentCurrency().then((val) => {
let res = value * currencies[val]['rate'];
resolve(res);
});
});
});
}
1 Like
pwespi
January 10, 2018, 12:26pm
15
You have to return a Promise, but there is no need to create a new one:
test(value: number): Promise<number> {
return this.storage.get('age').then((currencies) => {
this.getCurrentCurrency().then((val) => {
let res = value * currencies[val]['rate'];
return res;
});
});
}
useTest() {
this.test(5).then((res) => {
console.log(res);
});
}
@addwebsolution your code turns the results of this type
t {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
__zone_symbol__state
:
true
__zone_symbol__value
:
14.869259999999999
__proto__
:
Object
@pwespi this code return undefined value
pwespi:
test(value: number): Promise<number> {
return this.storage.get(‘age’).then((currencies) => {
this.getCurrentCurrency().then((val) => {
let res = value * currencies[val][‘rate’];
return res;
});
});
}
useTest() {
this.test(5).then((res) => {
console.log(res);
});
}
hello,
are you sure that the other parts like getCurrentCurrency() deliver valid results?
Best regards, anna-liebt
Yes yes
durvilchat:
test(value: number): Promise<number> {
return this.storage.get(‘age’).then((currencies) => {
this.getCurrentCurrency().then((val) => {
let res = value * currencies[val][‘rate’];
res have good value here
return res;
});
});
}
useTest() {
this.test(5).then((res) => {
console.log(res);
});
}
My problem is to return the value of res
You’re nesting Promises. Don’t. You are doing
Promise.then(_ => promise.then(_ => promise))
Instead do
Promise.then(_ => promise)
.then(_ => promise)
.then(_ => promise)
Chain them one after the other. Don’t put them inside one another.