1

Is it ok to do something like this?

I mean, Is return await new Promise() a valid expression as of below?:

  async pushImage(image, basePath) {
    const imgId = this.angularDatabase.createPushId();
    const route = `${basePath}${imgId}`;
    const imageRef = this.angularFireStorage.ref(route);
    
    return await concat( 
      imageRef.put(image.image).snapshotChanges().pipe(ignoreElements()),
      defer(() => imageRef.getDownloadURL())
    )
      .pipe(
        map(
          (url) => (
             { ...image, url }
          )
        )
      )
      .toPromise();
  }

8
  • If you await and then return I guess it will just return normal value not promise Commented Nov 2, 2020 at 5:48
  • Because the function is async it will always return a promise value! You don't technically need the return await and you could defer the await to the callee of pushImage Commented Nov 2, 2020 at 5:52
  • You don't need to use async await at all. Just return the result. Also, whatever you return from an async function is always a Promise. Commented Nov 2, 2020 at 5:52
  • Yeah. But async/await should be used together right?? Commented Nov 2, 2020 at 5:53
  • @mex ... yes, you need async to use await ... but since your only promise is the last statement, you don't need to use async/await for that function, the result will always be a Promise anyway - i.e. if there's only ONE promise, and it's the thing that is being returned, there's no need to await .... so there's no need for async Commented Nov 2, 2020 at 5:55

1 Answer 1

1

Anything returned from an async functions comes out as a promise.

For example:

async getDate() {
   return new Date();
}

will return a promise with the contents of a date.

An example of reading this overly complicated getDate() function would be:

getDate().then(date => {
   console.log("The date: ", date);
});

However, you can also return a promise. This makes your code above perfectly valid, except you don't need to include the await in the return.

You can "simply" just say

return concat( 
          imageRef.put(image.image).snapshotChanges().pipe(ignoreElements()),
          defer(() => imageRef.getDownloadURL())
       ).pipe(
          map(url => {
             return {...image, url}
          })
       ).toPromise();

Side note: I am not certain, but I think you should return the object in the map. Changing {...image, url} to return {...image, url}

Additional response:

To retrieve the contents of a promise, use the .then() method. Example:

myReturnedPromise.then(dataOfMyReturnedPromise => {
   //This code is called after the promise is resolved.
   console.log("The returned value is: ", dataOfMyReturnedPromise);
}).catch(error => {
   //If this code is called, the promise completed with an error
   //This might be executed if you're retreiving data from the internet
   //and your internet connection is too poor to get the data
   console.error("Error: ", error);
});

If this answered your question, please mark it as the accepted answer.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok. Thanks. is it ok to return a promise inside a then() {} block?
@mex - yes, it is, the Promise that the .then returns will "become" the Promise you return in a .then
@mex, I've extended my answer to include a response to your question. Also, if this answered your question, please consider marking this as the accepted answer (check mark button).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.