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?
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)
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
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)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 2 years ago.
I am a newbie in JavaScript, trying to implement code to connect to MySQL. I am using Promise along with Async/Await function. My goal is to get the return value of the Promise object.
However, I have read and tried every possible way but still failed to understand how the Asynchronous function works in JavaScript as the return value of the function getData() still return an object
Promise { < pending > }
I also tried to use then but it still returns the same result. I would really appreciate it if someone can show me what I have misunderstood or where I did wrong in this function. Thank you!
Here is my JS code:
function test_connection() {
var connection = #MySQLConnectionObject;
return new Promise((resolve, reject) => {
connection.connect((error, result) => {
if (error) {
reject(error)
} else {
connection.end();
resolve("Successfully Connected!");
}
});
});
}
async function getData() {
var result = await test_connection()
return result;
}
var result = getData();
console.log(result);
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)
})()