簡體   English   中英

promise.all 中的 Promise 不返回值

[英]Promise in promise.all doesn't returns value

我想在“ queryPexels ”下方 function 返回 imgSuggestions 變量的值,這不會發生(已編輯)我粘貼了代碼的 rest,我相信問題出在 ZB321DE3BDCD79 all 的語法中



  let foundProduct; //product id
  let imgSuggestions; //pexels
  let labelSeggestions; // OpenAI
  let headingSuggestions; // OpenAI Title
  let fontDownloadLink; // Font link

  if (
    foundRegistries.products.some((product) => product.id === productId)
  ) {

    foundProduct = foundRegistries.products.filter(
      (product) => product.id === productId
    );
  
    let queryImage = foundProduct[0].productName.toString()
    return new Promise(async (resolve, reject) => {
      try {
        await Promise.all([
          (imgSuggestions = await queryPexels(
            pexelsConfig,
            queryImage,
            photosPerPage,
          )),
          (labelSeggestions = await queryOpenAiLabel(
            openAiConfig,
            foundProduct[0].productName,
            foundProduct[0].targetAudience
          )),
          (headingSuggestions = await queryOpenAiHeading(
            openAiConfig,
            foundProduct[0].productName
          )), 
          (fontDownloadLink = await getFonts(
            googleConfig,
            foundRegistries.font,
            foundRegistries.fontVariant
          )),
        ]).then(() =>
          resolve(
            {
              productId : productId,
              imgSuggestions : imgSuggestions,
              labelSeggestions : labelSeggestions,
              headingSuggestions : headingSuggestions,
              fontDownloadLink : fontDownloadLink,
            }
          )
        );
      } catch (error) {
        throw error;
      }
    });
  } else {
    return {
      Error: "No product found. Please check provided product ID.",
    };
  }

這是她我的 function 異步queryPexels

async function queryPexels(pexelsConfig, query, photosPerPage) {
  // const client = createClient(pexelsConfig.key);
  queryOpenAiKeywords(
    invocation.openAiConfig,
    query
  ).then(res => {
    const query = res
    translateTxt(query).then(translate =>{
      const trad = translate.toString()
      const client = new Client({ apiKey: pexelsConfig.key});
      getPexelsPhotos(trad,client,photosPerPage).then(response =>{
        const photos = choosePhotos(response.photos)
        console.log(photos) // THIS PHOTOS IS PRINTED BUT IMGSUGGESTIONS IS EQUAL TO UNDEFINED
        return photos // BECAUSE THIS RETURNS ISN'T GET OF BY PROMISE ALL
      }).catch( err => {
        console.log(err)
      })
    }).catch( err => {
      console.log(err)
    });
  })

}

當我在 Promise.all 之后打印 imgSuggestions 時,我得到了未定義的值,但是 queryPexels console.log(photos) 成功了。

添加了缺少的退貨,應該可以解決問題

async function queryPexels(pexelsConfig, query, photosPerPage) {
  // here
  return queryOpenAiKeywords(
    invocation.openAiConfig,
    query
  ).then(res => {
    const query = res
    // here
    return translateTxt(query).then(translate =>{
      const trad = translate.toString()
      const client = new Client({ apiKey: pexelsConfig.key});
      // here
      return getPexelsPhotos(trad,client,photosPerPage).then(response =>{
        const photos = choosePhotos(response.photos)
        console.log(photos)
        return photos
      }).catch( err => {
        console.log(err)
      })
    }).catch( err => {
      console.log(err)
    });
  })

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM