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);
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
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.