Inside async function, returning value from a callback function returns Promise(undefined)
Date : March 29 2020, 07:55 AM
will be helpful for those in need I'm new to asynchronous programming, I'm facing issue similar to this question, in this question suggested approach uses callbacks but I'm trying to do it using Promises and async-await functions. I get undefined in the console. Here's my example. what am I missing? , make your query function return a Promise function query(sql, args) {
return new Promise(function (resolve , reject) {
this.connection.query(sql, args, (err, rows) => {
if (err)
reject(err);
else
resolve(rows)
});
});
}
//calling the function here
query("select 1")
.then((row) => console.log("Rows",row)) // Rows undefined
.catch((e) => console.log(e));
|
function keeps returning undefined array using Async Await
Date : March 29 2020, 07:55 AM
it helps some times The way to return the promise is not by adding new Promise (which is an anti-pattern in this case), but to get the promise that jQuery already has for you: return $.getJSON(url, function(data) {
// ^^^^^^
// .......
}).promise();
// ^^^^^^^^^^
return $.getJSON(url).then(function(data) {
// ^^^^^^ ^^^^^^
// .......
return levels;
});
|
Async function is returning undefined
Date : March 29 2020, 07:55 AM
it should still fix some issue You do not return anything from your openChat function, so that function resolves to undefined. You have to write: export async function openChat(otherPersonId) {
const user = firebase.auth().currentUser
const userId = user.uid
return firestore // here you need to return the returned promise of the promise chain
.collection('users')
.doc(userId)
.get()
/* .... */
}
async getChatId(userId) {
let chatId = await openChat(userId)
console.log('chatId', chatId) //UNDEFINED
return chatId
}
async requestChat(userId) {
let result = await this.getChatId(userId)
console.log('result', result) //UNDEFINED
}
|
Async function returning undefined instead of data
Date : March 29 2020, 07:55 AM
With these it helps I'm doing requests to my API server to authenticate a user, that's not the problem. The problem is that I don't know why my async function doesn't return anything, and I get an error because the data that I want from this function is undefined. , Your Async function must return the last promise: return fetch('http://api.myapi.cc/authenticate', ...);
var x = await fetch('http://api.myapi.cc/authenticate', ...);
// do something with x and...
return x;
function a() {
return functionReturningPromise().then(function (result) {
return result + 1;
});
}
async function b() {
return (await functionReturningPromise()) + 1;
}
|
Async function returning undefined in NodeJS
Date : March 29 2020, 07:55 AM
this one helps. You don't need to use callback function when you use async/await Try this code: obtenerCategoria = async(category) => {
const categoriaDB = await Categoria.findOne({ descripcion: category });
if (!categoriaDB) {
return res.status(400).json({
ok: false,
msg: 'Categoria no encontrada'
})
}
console.log(categoriaDB); // Works fine here
return categoriaDB
}
|