This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 months ago.
const loadUsers = async () => {
const res = await fetch('https://www.breakingbadapi.com/api/characters')
const json = await res.json()
return json
}
let chars = loadUsers()
Why does it return a promise ? If i try to add await keyword to load users i get an error? So what s the problem here?
Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
Related
This question already has answers here:
async/await implicitly returns promise?
(5 answers)
Why is my asynchronous function returning Promise { <pending> } instead of a value?
(9 answers)
Async function returning promise, instead of value
(3 answers)
How can I access the value of a promise?
(14 answers)
What are asynchronous functions in JavaScript? What is "async" and "await" in JavaScript?
(2 answers)
Closed 5 months ago.
I just wanted to get random user data from some API then i used async function so that at last i can return person name array.
Here is my code
const getPersonName = async () => {
// Fake user Data api
const response = await fetch("https://dummyjson.com/users");
const data = await response.json();
const firstName = data.users.map((currUser) => currUser.firstName);
const lastName = data.users.map((currUser) => currUser.lastName);
return [...firstName, ...lastName];
};
after creating the function i have to store the returning value into names variable then i tried this.
const names = async () => await getPersonName();
when i console.log(names) it is still giving me promise
i wanted to store like this
const names = ['Some name','some name']
If you want to call an async function at the top level, you can wrap your code in an async IIFE:
(async () => {
const names = await getPersonName();
})();
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Use Async/Await with Axios in React.js
(3 answers)
Closed 1 year ago.
I am new to this whole async function concept. Here is the function that I want to use:
async function fetchData() {
try {
const resultRes = await fetch("https://www.breakingbadapi.com/api/characters?category=Better+Call+Saul");
const result = await resultRes.json();
return result;
} catch (error) {
console.error(error);
}
}
And this is what function call looks like:
const Data = fetchData();
Now I want to console.log(Data) with the array that is returned but intead it shows as a promise object.
How should I use this function as using it with .then messes up my whole app as the containing file is a react component?
This question already has answers here:
Async function returning promise, instead of value
(3 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
The following code logs [Object Promise] instead of a string which is a thing I don't want.
I've tried to make them both async function yet that didn't work.
...
let URL = searchYouTube(arg)
console.log(URL)
...
searchYouTube function:
async function searchYouTube(args) {
youtubes(args, function (err, r) {
let video = r.videos
let Fvideo = video[0]
console.log(Fvideo.url)
return String(Fvideo.url)
})
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I'm trying to make a function where I can easily call my MongoDB.
This is the function code/hanlder:
let get = {};
get.getGuildData = (id) => {
const guildData = require('./models/guilds.js')
guildData.findById(id).then(async (data) => {
return guildData.findById(id)
})
};
module.exports = { get }
This is where I am calling the function:
const getGuild = bee.get.getGuildData(msg.guildID)
console.log(getGuild)
It returns undefined, but the console.log on the actual function returns the correct thing:
Let me know if anyone knows a solution to this.
I can not find an answer in this post. How do I return the response from an asynchronous call?
You cannot return the data, because it doesn't exist yet. What you can do is return a promise. guildData.findById(id) apparently is already returning a promise which resolves to the data, so you do not need to call .then on it to create a new promise.
get.getGuildData = (id) => {
const guildData = require('./models/guilds.js')
return guildData.findById(id);
};
Since the function is now returning a promise, any code that calls it will need to work with promises. Either call .then on the promise:
bee.get.getGuildData(msg.guildID)
.then(data => {
console.log(data);
});
Or if you're in an async function, use await:
async function someFunction() {
const data = await bee.get.getGuildData(msg.guildID);
console.log(data);
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
Hello I have a function that goes to the database and returns array of objects like so:
function findAlbumImages(){
remote.findAlbum.then(
res =>{
})
}
but I want to call this from another function and assign that res to array collection like so:
let newArray = findAlbumImages();
Is there a way to do this?
Sure you can, by using async/await, which is the closest you can
get to your desired syntax:
function findAlbumImages() {
return remote.findAlbum()
}
(async () => {
let newArray = await findAlbumImages()
console.log(newArray)
})()