This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my asynchronous function returning Promise { <pending> } instead of a value?
(9 answers)
async/await implicitly returns promise?
(5 answers)
Async function returning promise, instead of value
(3 answers)
Closed last year.
I am getting stuck on a Mongoose function. The goal of the function is to:
Store DB query in the query variable
Return True IF there is a DB query matching my criteria. False, otherwise.
The code looks something like:
let query = function (query) {
return new Promise((res, rej) => {
res(UserModel.findOne({ username: toString(query) }));
})
.then((user) => {
return user;
})
.catch((rej) => {
console.log(rej);
});
};
let result = await query(usernameToQuery);
if (result == null) {
return true;
} else {
return false;
}
No matter what I do, this code will never return after the query statement has been resolved. All it ever returns to the calling function is promise <pending>. How would I fix this?
Related
This question already has answers here:
Why do I need to await an async function when it is not supposedly returning a Promise?
(3 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
This is my first time using promises and mongoose, and I'm trying to save data to global variable for later use
const getUser = async () => {
let user
try {
user = await clientModel.findOne({username: email})
consoe.log(user)
} catch (e) {
console.log(e)
}
return user
}
const filteredUser = getUser().then((value) => {
return value
}).catch((e) => console.log(e));
console.log(filteredUser)
user console log displays content:
{
_id: new ObjectId("61aa75c64e1526131d98f2a1"),
username: 'paul#beatles.uk',
chequing: null,
saving: '1000022',
__v: 0
}
but filteredUser keeps displaying Promise { <pending> }
You need to resolve promise for use data. Read about that here
It can be await or then
This question already has answers here:
How can I access the value of a promise?
(14 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I'm building backend to fetch pictures of users I follow. The function below is a helper function that takes an array of user IDs to map against it to get their pictures. This function returns a single promise.
const getFeedPhotos = async (followingArr) => {
const promises = followingArr.map(async userId => {
await Photo.find({userId: mongoose.Types.ObjectId(userId)});
});
const x = Promise.all(promises);
return x;
};
Code below is the router itself where I am invoking my helper function.
photoRouter.get(
'/feed',
expressAsyncHandler(async (req, res) => {
const following = req.body.following;
if (following) {
const response = getFeedPhotos(following);
console.log(response);
} else {
res.status(400).send({ message: '"Following" is not provided.' })
}
})
);
The problem is that response equals to Promise { <pending> }. I need this promise to resolve in order to send it to my client. My question is how do I do that? I do realize that it has to do with some blunder of mine and stems from my misunderstanding of the whole async concept per se. I was trying to make some research but none of the solutions worked for me.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How can I access the value of a promise?
(14 answers)
Closed 1 year ago.
I'm trying to write an async function that returns the response from the find ISS API using axios.
What's happening is whatever I ty to return winds up as a pending promise.
If I try to access the information in the function with console.log that works
async function apiCall(url) {
try {
const response = await axios.get(url);
console.log(response.status)
return response;
} catch (error) {
console.error(error);
}
}
console.log(apiCall("http://api.open-notify.org/iss-now.json"))
and it logs
Promise { <pending> } 200
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I can't work out how to return the nested .then methods, and resolve the value to the function that called it?
I can print it, at the deepest level of the last .then(), but I would like to print the returned value in the event listener.
connectedCallback () {
this.input.addEventListener('input', event => {
this.search(this.input.value)
})
}
search (str) {
let searchResult = window.fetch(`http://localhost:3000/api/?q=${str}`)
.then(result => {
return result.json()
.then(result => {
return result
})
})
}
}
Use async/await to await the promise result in the event listener. You can also simplify your promise chain considerably:
connectedCallback() {
this.input.addEventListener("input", async event => {
const result = await this.search(this.input.value);
console.log(result);
});
},
search(str) {
return window
.fetch(`http://localhost:3000/api/?q=${str}`)
.then(result => result.json());
}
I would recommend reading up on promises and getting familiar with how they work.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I have the following promise
let oembedData = oembed(uuid)
.then(res => res);
console.log(oembedData);
What I'm hoping to achieve is to return the res to oembedData so the value can be used.
I know I can do
let oembedData;
oembed(uuid)
.then((res) => {
oembedData = res;
console.log(oembedData);
});
but I feel this way isn't as clean as the former example.
When I use the former my console log returns Promise {$$state: {…}}
If you're free to use async/await, it looks almost like you'd want.
async function oembed(uuid) {
return new Promise(resolve => setTimeout(() => {
resolve('foo');
}, 3000));
}
async function getOembed() {
try {
const oembedData = await oembed('1234');
console.log(oembedData);
} catch(err) {
handleError(err);
}
}
getOembed();