Reputation: 4972
I am having the following function that contain a promise:
async checkValueIfExists(searchKey, appendedUrl, keyValue, ou, program) {
const checkValuePromise = new Promise((resolve, reject) => {
this.api.getData(this.globalVar.basicSearchFieldUrl + appendedUrl, 'dhis').subscribe(
(resp) => {
resolve(resp);
// return resp;
},
(error) => {
reject();
console.log(error);
}
);
});
}
And here I tried to catch the reponse:
async uploadData() {
let awaitCheckValue = await this.checkValueInDhis2(searchValue, appendToUrlEqual, keyField, ou, programId).then((res) => {
console.log(res)
}).catch(res=>console.log(res));
}
The consoled value is always undefined, but when I console it at the function of checkValueIfExists()
the response appeared.
I tried some solution on stack overflow but still the same.
Here is a simplified stackblitz about the situation.
Upvotes: 0
Views: 40
Reputation: 691
Thats because you are resolving something from promise but never returning anything from your async function.
You will need to replace this.
const checkValuePromise = new Promise((resolve, reject) => {
to
return new Promise((resolve, reject) => {
This will work and I have tested on your stackblitz. https://stackblitz.com/edit/simplified-promise-vj7d1q?file=src/app/app.component.ts
Upvotes: 1