-2

Can i reduce these functions that works together in only one? If i can do that, how?

Edit: createMedia function need to upload a media file in wordpress, instead createMediaAsync works for await to complete the process of uploading although it is not important for this question.

const createMedia = (postImgGalleryToUpload) => {
  return wp.media()
    // Specify a path to the file you want to upload, or a Buffer
    .file(postImgGalleryToUpload)
    .create({
      title: 'My awesome image',
      alt_text: 'an image of something awesome',
      caption: 'This is the caption text',
      description: 'More explanatory information'
    })
    .then(function (response) {
      return response.link;
    });
}

const createMediaAsync = async (postImgGalleryToUpload) => {
  try {
    var linkImage = await createMedia(postImgGalleryToUpload);
    console.log("IMMAGINE INSERITA" + linkImage);
  } catch (error) {
    console.log("Errore in createMediaAsync() - " + error);
  }
} 

In my main file i have the call to the async one:

createMediaAsync(postwp.postImgGallery1);

Thank you who everyone can give me a tip.

For make the question clearer I would like to do something like this:

const createMedia = async (postImgGalleryToUpload) => {
    return await wp.media()
        // Specify a path to the file you want to upload, or a Buffer
        .file(postImgGalleryToUpload)
        .create({
            title: 'My awesome image',
            alt_text: 'an image of something awesome',
            caption: 'This is the caption text',
            description: 'More explanatory information'
        })
        .then(function (response) {
            return response.link;
        })
        .catch(function (error) {
            return error;
        });
}

I know that is wrong in fact it doesn't work at all, is there an alternative.

Thank you

0daycode
  • 11
  • 3
  • It's not clear what the issue is--sure, although as written now, your code shouldn't work anyway since `createMedia` isn't returning the link. You may want to look at [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992) and spin through some docs/tutorials about async JS in general. – Dave Newton May 12 '21 at 16:33
  • Sorry i wasn't clear, the central question is how to wait for a promise within the same function, then do await within the same function and return the resolved promise. This code works well. I'm going to change the question for clarify the question – 0daycode May 12 '21 at 16:39
  • Sorry, I missed the `await` inside `createMediaAsync`. You do it the exact same way. – Dave Newton May 12 '21 at 16:41
  • `createMediaAsync ` doesn't need to exist, it's doing (*almost*) exactly what the function it calls is doing. – Kevin B May 12 '21 at 16:41
  • Please look at the last code, what do you think I'm doing wrong? – 0daycode May 12 '21 at 16:46

1 Answers1

-1

Sure, you can literally just inline the function definition to replace the call:

const createMediaAsync = async (postImgGalleryToUpload) => {
  try {
    var linkImage = await wp.media()
      .file(postImgGalleryToUpload)
      .create({
        title: 'My awesome image',
        alt_text: 'an image of something awesome',
        caption: 'This is the caption text',
        description: 'More explanatory information'
      })
      .then(function (response) {
        return response.link;
      });
    console.log("IMMAGINE INSERITA" + linkImage);
  } catch (error) {
    console.log("Errore in createMediaAsync() - " + error);
  }
} 

However, you should not mix async/await syntax with .then(…) syntax! Use either

function createMediaAsync(postImgGalleryToUpload) {
  return wp.media()
    // Specify a path to the file you want to upload, or a Buffer
    .file(postImgGalleryToUpload)
    .create({
      title: 'My awesome image',
      alt_text: 'an image of something awesome',
      caption: 'This is the caption text',
      description: 'More explanatory information'
    })
    .then(response => {
      const linkImage = response.link;
      console.log("IMMAGINE INSERITA" + linkImage)
    }, error => {
      console.log("Errore in createMediaAsync() - " + error);
    });
}

or

async function createMediaAsync(postImgGalleryToUpload) {
  try {
    const response = await wp.media()
      .file(postImgGalleryToUpload)
      .create({
        title: 'My awesome image',
        alt_text: 'an image of something awesome',
        caption: 'This is the caption text',
        description: 'More explanatory information'
      });
    const linkImage = response.link;
    console.log("IMMAGINE INSERITA" + linkImage);
  } catch (error) {
    console.log("Errore in createMediaAsync() - " + error);
  }
} 
Bergi
  • 630,263
  • 148
  • 957
  • 1,375