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