Await fetch not working as expected in Javascript [duplicate] - javascript

This question already has answers here:
async/await implicitly returns promise?
(5 answers)
Async function returning promise, instead of value
(3 answers)
Closed 1 year ago.
Why output from code below is PromiseĀ {<pending>} instead of Response.
What I doing wrong? Also await fetch("https://...") not working.
function asynchronousFunction() {
return fetch("https://thatcopy.pw/catapi/rest/")
}
const mainFunction = async () => {
const result = await asynchronousFunction()
return result;
}
console.log(mainFunction())

You shouldn't think of Promises and async functions (which return Promises) as something you can use to return a value at top-level. It's usually a bad idea that comes from the imperative and synchronous programming mindset.
Use chaining instead, either move the console.log inside mainFunction, or write this:
asynchronousFunction()
.then(console.log)

Related

How to use promise on callback from proprietary software [duplicate]

This question already has answers here:
How to "await" for a callback to return?
(5 answers)
How do I convert an existing callback API to promises?
(24 answers)
Closed last year.
I'm using a software that lets me read a data on it's server using this function :
webMI.data.read(nodeID, function(e){...})
Passes a current property object for every data variable specified by its nodeID to the callback function. You can pass a single nodeID or an array of nodeIDs.
In the callback i get the value i want, but i couldn't get it out and didn't want to make a callback hell
After some reading/testing on asyncronous function, i tried using promise but i keep getting the same error and i don't understand why
webMI.data.read("AGENT.OBJECTS.motor.isActive",e => e.value)
.then(result => {
console.log(result)
})
Uncaught TypeError: webMI.data.read(...) is undefined
I also tried to put it in an async function and using await
async function GetValue(){
var temp = await webMI.data.read("AGENT.OBJECTS.motor.isActive",function(e){return e.value})
return temp
}
But temp always return a Promise with state = fullfilled and value = undefined
My goal is to get 1 to X values this way to make a formula
Note that i have no way to modify 'webMI.data.read'
Use the promise constructor and resolve from within the callback
function GetValue(){
return new Promise(resolve => {
webMI.data.read("AGENT.OBJECTS.motor.isActive",function(e){resolve(e.value)})
})
}
Now the GetValue function is async, as it returns a promise and can be awaited.
How to "await" for a callback to return?

Pause function until promise is resolved [duplicate]

This question already has answers here:
How to wrap async function calls into a sync function in Node.js or Javascript?
(9 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
What is the JavaScript version of sleep()?
(91 answers)
Closed 1 year ago.
My function keeps executing indefinitely, although I would have expected it to resume after 10ms. Is there any way I can flush timeouts while I am waiting for the result? Or at least tell JS to execute / resolve other promises before I return my result?
The simple matter is, I NEED the response before I can continue execution. And I can not put my code in an async block whatsoever. I also can not use 'then' since I need to return the raw result. I thought this hack would work, but nope.
As far as my understanding of JS goes, this simply is not possible, but I would love to know.
let test = function () {
let wait = true;
setTimeout(() => {
wait = false
}, 10);
while (wait);
return wait;
}
you can use async/await , to block the flow of the function
example
const mine = async() => {
console.log("start")
await anotherAsyncFunction()
console.log("done")
}

Is it possible to get a value from a JavaScript immediately-invoked arrow function by using a "return" statement? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
In this code example:
let result = (async (global) => {
// other code here (includes await's; thus the async)
return 123;
})(this);
The code works, but the return'ed value is nowhere to be found (not in result). Is there any way to use a normal return statement to get data out of this function?
Since you have used an async function it returns a promise instead of a value.
Try the following:
var result = (async (global) => {
// other code here (includes await's; thus the async)
return 123;
})(this);
result.then((res)=> console.log(res));

calling a async function inside then [duplicate]

This question already has answers here:
Removing nested promises
(2 answers)
Closed 7 years ago.
I have a piece of code that looks like this:
func().then(function (result){
var a = func1(result);
func2(a).then(function(result1){
////
}
}
As you can see func returns a promise, and in then part we call another func1 which also returns a promise. Is it possible to chain the promise returned from func2 with the promise of then, and somehow get ride of the nested functions in the second then.
The return value inside a then() function is used as a promise value itself. So you can easily return the new promise and keep on chaining here:
func()
.then(function (result){
var a = func1(result);
return func2(a);
})
.then(function(result1){
////
})
See 2.2.7 and 2.3.2 of the Promise A+ Spec.

Promises Help - Returning Values [duplicate]

This question already has answers here:
setting a variable to get return from call back function using promise
(2 answers)
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
Closed 8 years ago.
I have a javascript function where I want to return the value that I get after the return method.
Easier to see than explain
function getValue(file){
var val;
lookupValue(file).then(function(res){
val = res.val;
}
return val;
}
What is the best way to do this with a promise. As I understand it, the return val will return before the lookupValue has done it's then, but the I can't return res.val as that is only returning from the inner function.
Use a pattern along these lines:
function getValue(file) {
return lookupValue(file);
}
getValue('myFile.txt').then(function(res) {
// do whatever with res here
});
(although this is a bit redundant, I'm sure your actual code is more complicated)
The best way to do this would be to use the promise returning function as it is, like this
lookupValue(file).then(function(res) {
// Write the code which depends on the `res.val`, here
});
The function which invokes an asynchronous function cannot wait till the async function returns a value. Because, it just invokes the async function and executes the rest of the code in it. So, when an async function returns a value, it will not be received by the same function which invoked it.
So, the general idea is to write the code which depends on the return value of an async function, in the async function itself.

Categories

Resources