JavaScript async/await makes 'undefined' [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How do I convert an existing callback API to promises?
(24 answers)
Closed 8 months ago.
I have a problem with a JavaScript function that it has async await. I create this with the goal of learning asynchrony.
// function that just solve an addition
function addition(num1, num2){
let sum = num1 + num2
return sum
}
// I treat this function as an api
function num(value=3){
setTimeout( function (){
let num = value;
return num
}, 3000)
}
// I put an async/await for waiting the response of 'num' function
const Exercise = async() => {
const num2 = await num(2)
console.log(addition(3,num2))
}
Exercise()
The num() function has a behaviour as an API what I've to wait it response, but when I run all of this code the first response is 'undefined' and then 'NaN'. I don't understand why.
Do you know what I'm doing wrong?
Thanks a lot!

Related

Callbacks with timeout in js [duplicate]

This question already has answers here:
What is a callback function?
(22 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I have across a block of code that I couldn't really tackle. It is written as following:
function getValue (callback) {
setTimeout(() => {
callback("B")
}, 10)
}
How can we call this function to return the string, "B" as it has a callback inside a timeout of 10ms. Any help? I have tried calling it normally but it returns undefined. Can anyone explain how this block of code works?
--Here is the problem that was supposed to be solved. There are three functions that return A,B,C respectively as strings and the goal here is to get the output to be printed the same as above (A,B,C).
function getA () {
return "A"
}
const getB = async () => { const b = await "B"; return b }
function getC (callback) {
setTimeout(() => {
callback("C")
})
}

Prevent function from giving an object promise [duplicate]

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)
})
}

Async Function and Promise [duplicate]

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);

How do i synchronously get the result of a Promise [duplicate]

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)
})()

Node JS Promise won't return a value [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
The below code is not returning a value back but it console logs out fine. Any ideas how I can set the value of X?
var dbSize = dbo.collection('Items').count()
var x = 0
x = dbSize.then(len => {
return len
})
This is what is being logged 'Promise { }' however if I simply write this:
dbo.collection('Items').count()
var x = 0
dbSize.then(len => {
console.log(len)
})
then It logs out fine.
you can warp your code with an async function and then use await to make your code work like synchronized code
const a = async () =>{
var dbSize = await dbo.collection('Items').count()
console.log(dbSize)
}
a();

Categories

Resources