0

How to change the value of data inside the asyncCall() and set it to the resolve value of the promise which is b.

What I wanted is that the asyncCall() will return b. But what happen is that asyncCall() returns a Promise object.

function resolveAfter2Seconds(data) {
  return new Promise(resolve => {
    resolve(data);
  });
}

async function asyncCall() {
  let data = "a";
  var result = await resolveAfter2Seconds("b");
  data = result;
  return data;
}

asyncCall();
davecar21
  • 2,606
  • 21
  • 33
  • 1
    Use `.then()` to access the value. – Kobe Jun 11 '19 at 09:39
  • _What I wanted is that the asyncCall() will return b._ You just can't. Async functions always return a promise. – hindmost Jun 11 '19 at 09:44
  • What you want is eventually like [_returning response from asynchronous call_](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – hindmost Jun 11 '19 at 09:53
  • @hindmost What it did is that it console.log the variable rather than return it. – davecar21 Jun 11 '19 at 09:57

4 Answers4

3

Use IIFE

Making the function async will automatically have it return a Promise. You will either have to await it, or use .then to access the resolved value. IIFE stands for "Immediately Invoked Function Expression" – Tim VN

function resolveAfter2Seconds(data) {
  return new Promise(resolve => {
    resolve(data);
  });
}

async function asyncCall() {
  let data = "a";
  var result = await resolveAfter2Seconds("b");
  data = result;
  return data;
}

(async function() {
  console.log(await asyncCall())
})()
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69
  • 1
    This is he correct answer. Making the function ```async``` will automatically have it return a ```Promise```. You will either have to ```await``` it, or use ```.then``` to access the resolved value. @MattEllen IIFE stands for "Immediately Invoked Function Expression" – Tim VN Jun 11 '19 at 09:47
1

An async function will always return a Promise (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#Return_value).

If you put a console.log(result) statement in the asyncCall() function body, you will realize that the value is actually resolved right there and can be used as you'd expect it. But as documented, the return value of an async function will always be "promisified", so you'd have to "await" that function call too.

To do that at the top (global) level, you can either use an IIFE (Immediately Invoked Function Expression):

(async () => console.log(await asyncCall()))();

or fall back to classical callback functions using Promise.then():

asyncCall().then(value => console.log(value));

Some browsers also support top level await expressions:

await asyncCall();
loadmaster
  • 46
  • 3
0

declaring function with async will return classic Promise. so you have to use callbacks or await.

async function asyncCall() will return Promise not the result you want to.

asyncCall().then((data) => console.log('in data is your result'));
Juraj Kocan
  • 2,648
  • 1
  • 15
  • 23
0

Async functions return a promise itself:

function resolveAfter2Seconds(data) {
  return new Promise(resolve => {
    return resolve(data);
  });
}

async function asyncCall() {
  const result = await resolveAfter2Seconds("b");
  return result;
}

asyncCall().then(
  data => console.log(data)
);
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43