-1

This is my code as of now:

function doquery(select,from,where,value){
  return new Promise((resolve, reject) => {
    con.query("SELECT " + select + " FROM " + from + " WHERE " + where + " = " + value, (err, res, fields) => {
      resolve(res);
    });
  });
}; 

const username = async function() {
  const data = await doquery('name','members','id',1);
  return (data);
};
    
username().then(v => {
  console.log(v); 
}); 

what I want is to be able to have console.log(v) OUTSIDE the function and still produce the same result, something like:

console.log(username);

is it possible?

Thank you.

David
  • 208,112
  • 36
  • 198
  • 279
Cain Nuke
  • 2,843
  • 5
  • 42
  • 65
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – David Sep 20 '21 at 18:07
  • To get the result of a `Promise`, your options are basically to `await` it or append a `.then()` to it. So either `console.log(await username());` or what you currently have. – David Sep 20 '21 at 18:07
  • if I use `console.log(await username());` it says await can be used only in async functions. – Cain Nuke Sep 20 '21 at 18:18
  • That's correct, `await` can only be used in an `async` function. If this code is at the top level then you'd either wrap it in an `async` IIFE or you'd use `.then()`. – David Sep 20 '21 at 18:20
  • I dont want to wrap anything so how can use then? – Cain Nuke Sep 20 '21 at 18:22
  • What's the actual problem you're trying to solve? Do you have an example of something that isn't working? So far it sounds like all you're indicating is that you just don't like the syntax of Promises, which isn't really something we can change. – David Sep 20 '21 at 18:25
  • you mentioned it is possible to avoid all the wrappings by using .then. isnt? – Cain Nuke Sep 20 '21 at 18:26
  • 1
    Using `.then()` is exactly what you're already doing in the code shown. You could shorten it in this case to simply: `username().then(console.log);` But it's still not clear what you're asking or what you mean by "avoid all the wrappings". You either `await` or you append a callback with `.then()`. Those are your options. The code shown includes correct examples for both approaches. – David Sep 20 '21 at 18:29
  • so summarizing, its impossible to have a variable store the result outside the whole function? Is going to always be wrapped? – Cain Nuke Sep 20 '21 at 18:37
  • why cant I do this instead? `console.log(username().then);` – Cain Nuke Sep 20 '21 at 18:44
  • You certainly can do that, it just doesn't do the same thing as the code shown. In that case you're logging a function *itself* to the console. Nowhere in that example are you *invoking* the `.then` function or passing anything to it. – David Sep 20 '21 at 18:49
  • thats the problem. I only get `function then` and not the value I want. – Cain Nuke Sep 20 '21 at 18:51

1 Answers1

0

If your project supports top-level await (for example, node 14.17.1), you can do:

const username = async function() {
  const data = await doquery('name','members','id',1); // doquery must be async or return a promise (i.e fetch)
  return (data);
};
    
const myVar = await username();
console.log(myVar)

A working example:

const username = async function() {
      
  const data = await fetch('https://dog.ceo/api/breeds/image/random');
    
  return await data.json();
};
        
const myVar = await username();
console.log(myVar.message)
Skarlinski
  • 2,419
  • 11
  • 28
  • that throws an error that says: await is only valid in async function – Cain Nuke Sep 20 '21 at 18:46
  • Your doQuery function needs to be async or return a promise – Skarlinski Sep 20 '21 at 18:48
  • doquery function returns a promise. Please check it, I posted it on my question. – Cain Nuke Sep 20 '21 at 18:52
  • @Skarlinski: Your "working example" doesn't work, for exactly the reason the OP specified. `await` can only be used within an `async` function. To use `await` in top-level code you would have to wrap it in an `async` IIFE. Otherwise, instead of `await` you would append `.then()` to the returned `Promise`. (Which is exactly what the OP already does, and so far no actual problem with that code has been specified in the question...) – David Sep 20 '21 at 18:54
  • @David you are right, but top level awaits exists in many different scenarios, particularly in nodejs 14+, or some compiled JS. You can try running the working example in the devtools – Skarlinski Sep 20 '21 at 19:02