Prevent function from giving an object promise [duplicate] - javascript

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

Related

How to assign a fetch response to global variable? [duplicate]

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

JavaScript async/await makes 'undefined' [duplicate]

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!

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

Issue in using async/await function [duplicate]

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?

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

Categories

Resources