简体   繁体   English

NodeJs 异步等待 1 个调用而不是 2 个不同的函数

[英]NodeJs async await in 1 call instead of 2 distinct functions

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.编辑:createMedia function 需要在 wordpress 中上传媒体文件,而不是 createMediaAsync 用于等待完成上传过程,尽管这对于这个问题并不重要。

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谢谢

Sure, you can literally just inline the function definition to replace the call:当然,您可以直接内联 function 定义来替换调用:

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 !但是,您不应该将async / await语法与.then(…)语法混合使用 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);
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM