2
\$\begingroup\$

I'm having a hard time wrapping my head around whether or not this works or how I can even test it but....

I want to use Async/Await inside a Promise, which means, when I create a new Promise, I have to pass it an async function... something like this:

const getData = function(){
  return new Promise(async function(resolve, reject){
    try {
      const results = await axios.get(some_data);
      resolve(results);
    }
    catch(er) { reject(er); }    
  })
}

I've tried to test it myself, but I can't tell if it's working correctly or not. Anyone try this before?

\$\endgroup\$
5
\$\begingroup\$

There's simply no reason to wrap your existing promise with another promise and no reason to use await or async here either:

const getData = function(){
  return axios.get(some_data);
}

Wrapping an existing promise with a manually created promise is a promise anti-pattern and should not be done. Instead, you should just return the promise you already have.

If you're going to use more than one async operation and thus find you want to use await rather than chain your promises with .then(), then you can do that like this:

const getData = async function(){
  let val = await axios.get(some_data);
  let otherVal = await someOtherFunction();
  return something;
}

Note also that if you want a rejected promise to just get returned as a rejection, you don't have to catch it locally in an async function. If you one of the promises you are using await on rejects, that will just reject the promise that your async function returns with that error.

If you wanted to catch the error locally and do something about it, then you would use try/catch:

const getData = async function(){
  try {
    let val = await axios.get(some_data);
    let otherVal = await someOtherFunction();
    return something;
  } catch(e) {
    // some something with the error and decide what to return
  }
}
\$\endgroup\$
  • \$\begingroup\$ I was about to make a comment about the try/catch but it looks like you've already done so. +1. \$\endgroup\$ – Andrew Li Dec 22 '17 at 3:38
  • \$\begingroup\$ You are both correct. I'm not using axios, that was just for the example, but I looked into it and the function I'm calling does indeed return a promise already. I was lost in all the module separation I'm doing for my first node app and forgot the details of what each function was doing. Lesson learned. Thank you for you input!! \$\endgroup\$ – TJBlackman Dec 22 '17 at 3:46
1
\$\begingroup\$

Your code doesn't make sense. axios.get already returns a promise that resolves with the data fetch -- there's no need for a promise wrapper at all, nor async/await:

const getData = function() {
  return axios.get(some_data);
}

Then in another function, if you depend on some result you get from getData, then use async/await there:

const processData = async function() {
  //if you were to use try/catch, you would wrap it around this await/other awaits here
  const result = await getData();
  //now you can use result as you like
}

You should never use an async callback with a Promise. That would suggest you're wrapping a promise in a promise, which doesn't make any sense. If you're using await with an asynchronous call inside a Promise callback, just return the asynchronous call -- it's mostly likely it's already returning promise (or else using await in the first place wouldn't make sense).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.