0

I learned about the concepts of Async and Await. I attempted to use it but I am not getting the results I would like.

My async function:

let get_server_resource = async () => {
    let a = await x();
    let b = await b();
    return {
        a: a, b: b
    }

}

Function call located in another scope.

let main = async () => {

    result = await get_server_resource();
    console.log(result);
}

However, this does not work. How can I utilize await and async correctly?

Thank you.

JunheePark
  • 35
  • 7
  • _"But it was not working synchronously"_ Why should it work synchronously when you are using `async` and `await` – Maheer Ali Jan 21 '20 at 16:29
  • "How can I use it gently" - I don't know how, but I'd love to find out. – evolutionxbox Jan 21 '20 at 16:30
  • `main()` is still an async function. Using `await` doesn't change that. – Barmar Jan 21 '20 at 16:30
  • It's not possible to turn an async function into sync. – Barmar Jan 21 '20 at 16:32
  • It was a way to solve the problem that get a result of the async function. I just wanna get the result synchronously at the main function. – JunheePark Jan 21 '20 at 16:32
  • Does this answer your question? [Get the value of a Javascript Promise in a synchronous way](https://stackoverflow.com/questions/37220554/get-the-value-of-a-javascript-promise-in-a-synchronous-way) – ponury-kostek Jan 21 '20 at 16:34
  • It's a bit hard without a fiddle but try this: let main = () => { result = get_server_resource().then(console.log(result)); } – feedy Jan 21 '20 at 16:39
  • 1
    What do you mean "this doesn't work"? Was there an error message in console? Is the result not what you expected? The way I see it, what you wrote should work, as far as the async functions are concerned. – Addis Jan 21 '20 at 16:43
  • @Addis it shows undefined. – JunheePark Jan 21 '20 at 16:44
  • @ponury-kostek Thank you for your link. but It is not my answer. – JunheePark Jan 21 '20 at 16:46
  • @JackPark what are `x()` & `b()`? – Addis Jan 21 '20 at 16:47
  • @Addis Both are async function which is get resource about the server status. I used node-os-utils Package. And the functions are cpu.usage(), mem.info(). – JunheePark Jan 21 '20 at 16:49
  • 1
    @Addis I just saw that. – Patrick Roberts Jan 21 '20 at 16:57
  • @JackPark, what do you get when you console.log `a` and `b` in the first function? – Addis Jan 21 '20 at 17:06
  • 1
    @Addis Wait, I solved it. I don't know how is done. but It works right now. Thank you. – JunheePark Jan 21 '20 at 17:13
  • 1
    @Addis Thank you for your help. This code is what I want to make. function "a" is get_process_resource(), function "b" is get_server_resource, and main is "monitor" function. https://github.com/JuneJobs/resource-monitor/blob/master/index.js – JunheePark Jan 21 '20 at 17:20

2 Answers2

1

aysnc functions don't return a value they return a Promise

so you can't get the value from them unless you await them this can be done in various ways the await keyword is the easiest and inside your async code you can always call a sync function

async useServerData()
{

    result = await get_server_resource();
    console.log(result);
    processServerResource(result)

}
processServerResource(resource)
{
    do Something
}

this has the advantage of not blocking your code

otherwise you can use the the then method to set a callback that you want to be called on completion

get_server_resource().then(processServerResource);

a really bad option would be to declare result outside the scope of your promise and then keep checking to see if it has been set

let result = null;
let main = async () => {

    result = await get_server_resource();
    console.log(result);
}
main();
while(result ===null)
{
}
processServerResource(result)

again this is not a recommended solution

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Methods

MikeT
  • 5,398
  • 3
  • 27
  • 43
0

If I get you right the problem is when you call your Main() function. Your Main() function actually returns a Promise and you should handle it as so even if the result is not interesting to you. So from a non-async function you call Main like:

Main()
    .then((result) => { /* ...continue */ })
    .catch(error) => { /* ...what happened if an error occurred */ }); 
Itay Merchav
  • 954
  • 8
  • 8