1

I have an async function. And I want to create an object and the value of the properties in the object is the return of the async function. But now The value of the property is an Object Promise

my code is like this:

const promise1 = new Promise(function(resolve, reject) {
  resolve('myvalue');
});
const a = async () => {
  var b = await promise1
  return b;
}
const newobj = {'a': a()};

console.log(newobj)

// the result is : {a: Promise {<resolved>: "myvalue"}} but i want {a: "myvalue"}
andryjohns
  • 21
  • 1
  • 2
  • You cannot do that. You can use `.then()` with the returned promise and construct your value in the callback. – Pointy Apr 09 '19 at 12:53
  • Wrapping a promise into an `async` function does not make the result available synchronously. – deceze Apr 09 '19 at 13:54

3 Answers3

1

The problem with your code is you are assigning return of a (which is an async function, so it will return a Promise) to newObj.a

An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value. You can read more about it here

Remember, the await keyword is only valid inside async functions. If you use it outside of an async function's body, you will get a SyntaxError.

Approach 1

const promise1 = new Promise(function(resolve, reject) {
  resolve('myvalue');
});
const a = async() => {
  var b = await promise1
  return b;
}

const printData = async() => {
  const newobj = {
    'a': await a()
  };
  console.log(newobj)
}

printData()

EDIT: As requested by johnson andriatiana, I have wrapped the code in async IIFE function.

Approach 2:

(async() => {
  const promise1 = new Promise(function(resolve, reject) {
    resolve('myvalue');
  });
  const a = async() => {
    var b = await promise1
    return b;
  }
  const newobj = {
    'a': await a()
  };

  console.log(newobj)
})();
Ashish
  • 4,206
  • 16
  • 45
  • My problem is that i want to return a value in PrintData const promise1 = new Promise(function(resolve, reject) { resolve('myvalue'); }); const a = async() => { var b = await promise1 return b; } const printData = async() => { const newobj = { 'a': await a() }; return newobj } printData() – andryjohns Apr 09 '19 at 13:33
  • wrap everything in an async IIFE function and then await inside – Ashish Apr 09 '19 at 13:36
  • @johnsonandriatiana I have added the second approach in the answer. – Ashish Apr 09 '19 at 13:43
1

The problem here is that running a() will return a promise, so, in order to assign the result of the promise, you should wait for it to finish first.

Here is one possible approach:

const promise1 = new Promise(function(resolve, reject) {
  resolve('myvalue');
});
const a = async () => {
  var b = await promise1
  return b;
}
const run = async () => {
  const newobj = {'a': await a()};
  console.log(newobj);
}

run()


   
Tabare
  • 151
  • 3
0

You need to await your async function. Hence instead

const newobj = {'a': a()};

do

const newobj = {'a': await a()};

a() returns a promise, await a() is (will be) the value returned by that promise.

mbojko
  • 13,503
  • 1
  • 16
  • 26