简体   繁体   English

在forEach循环中使用Promises和Promise.all

[英]Using Promises and Promise.all Inside forEach Loops

I am amending a locally stored variable (data) using this function, and it works. 我正在使用此函数修改本地存储的变量(数据),并且它可以正常工作。 But because it's not setup with Promises yet, the data is retrieved after the page loads, so it is not shown. 但是由于尚未使用Promises进行设置,因此页面加载后会检索数据,因此不会显示。 I've gone through several iterations of this with attempts at using Promises to get this working properly, but haven't been able to get it right. 我已经经历了几次迭代,尝试使用Promises使其正常工作,但是一直无法正确完成。

I'm assuming there should be a Promise.all for the projects.map area, but then I'm not sure where it gets resolved, and how it's called. 我假设应该在projects.map区域中存在一个Promise.all,但是我不确定它在哪里解析以及如何调用。

function getProjects() {
  fetch('http://localhost:8888/wp-json/wp/v2/categories')
  .then(r => r.json())
  .then(categories => {
    categories.forEach(category => {
      data.forEach(service => {
        if(service.service_name == category.name) {
          fetch(`http://localhost:8888/wp-json/wp/v2/projects?
categories=${category.id}`)
          .then(r => r.json())
          .then(projects => {
            service.projects = projects.map(project => {
              // if true, add PROJECT to THAT service
                if(project.better_featured_image) {
                  return {
                    project_name: project.title.rendered,
                    project_desc: project.content.rendered,
                    project_img: 
project.better_featured_image.media_details.sizes.medium.source_url
                  }
                } else {
                  return {
                    project_name: project.title.rendered,
                    project_desc: project.content.rendered
                  }
                }
            })
          })
        }
      })
    })
  })
}

var data is set up like this so that each project needs to be attached to a service based on category. 像这样设置var数据,以便每个项目都需要基于类别附加到服务。

var data = [
  {
    service_name: 'Service 1',
    service_desc: 'Desc',
    service_img: 'Img URL',
    projects: [
      {
        project_name: 'Project 1',
        project_desc: 'Desc',
        project_img: 'Img URL'
      }
  }
]

Use Promise.all() 使用Promise.all()

function getProjects() {
    fetch('http://localhost:8888/wp-json/wp/v2/categories')
        .then(r => r.json())
        .then(categories => {
            var promises = [];
            categories.forEach(category => {
                data.forEach(service => {
                    if (service.service_name == category.name) {
                        promises.push(fetch(`http://localhost:8888/wp-json/wp/v2/projects?categories=${category.id}`)
                            .then(r => r.json()));
                    }
                })
            })
            return Promise.all(promises)
        }).then(data => {
            // Get result of all call 
        })
}

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

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