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")
})
}
Related
This question already has answers here:
How to make a promise from setTimeout
(8 answers)
How to handle multiple async requests?
(2 answers)
Closed 6 months ago.
this is regarding async await and promises. I am not very good at this so I guess i´m missing something. My code is meant to execute the first greetings function which it does. But then only once its done (and only then) it should execute any consecutive functions.
I have tried setting it up in many ways but either the second function doesn't execute or it executes before completion of the first.
Here is my code:
const greetingDivs = [firstDiv, secondDiv, thirdDiv, fourthDiv, fifthDiv]
let y = 0
function greetings() {
if (y != greetingDivs.length) {
setTimeout(() => {
consoleOutput.appendChild(greetingDivs[y])
y++
}, 500)
setTimeout(() => {
greetings()
}, 500)
}
if (y == greetingDivs.length) {
return console.log('Greetings are done!')
}
}
function secondFunction() {
console.log('I have waited')
}
async function consoleOn() {
const result = await greetings()
if (result) {
secondFunction()
}
}
consoleOn()
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!
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)
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 6 years ago.
Here is the test for the function I am trying to write.
var chai = require('chai');
var doLater = require('../05-callback');
var expect = chai.expect;
describe('05-callback', function () {
it('Can get secret number', function (done) {
doLater(function (secret) {
expect(secret).to.equal(1337);
done();
});
});
});
I have written code that log's a message to the console asynchronously however, this code does not return the 1337 value asynchronously.
function doLater(secret) {
var code = 1337;
(function setImmediate(code) {
console.log("I will be executed immediately");
return code;
})(code);
setTimeout(function secret(code) {
console.log("I will be executed in 2 seconds.");
return code;
}, 2000);
}
Please let me know if this post is not clear and I will edit it. Forgive me if this post is redundant, the simular post's I found were to advanced for me. Thank you for your help!
In order to do what you want to do, you need to use a callback. Example:
function doLater(callback) {
setTimeout(function(){
//after 2 seconds call the callback
return callback(1337);
}, 2000)
}
//pass as param a function that will be called
doLater(function(result) {
console.log(result) //1337
})