0

I have the following promise

let oembedData = oembed(uuid)
    .then(res => res);

console.log(oembedData);

What I'm hoping to achieve is to return the res to oembedData so the value can be used.

I know I can do

let oembedData;
oembed(uuid)
    .then((res) => {
        oembedData = res;
        console.log(oembedData);
    });

but I feel this way isn't as clean as the former example.

When I use the former my console log returns Promise {$$state: {…}}

1 Answers1

2

If you're free to use async/await, it looks almost like you'd want.

async function oembed(uuid) {
  return new Promise(resolve => setTimeout(() => {
    resolve('foo');
  }, 3000));
}

async function getOembed() {
  try {
      const oembedData = await oembed('1234');
      console.log(oembedData);
  } catch(err) {
      handleError(err);
  }
}

getOembed();