迭代数据时嵌套的api调用

时间:2017-12-29 16:13:32

标签: reactjs react-redux superagent

我按照与"A simplified approach to calling APIs with redux"相同的步骤进行api通话。

然而,当我得到我的结果时,我试图进行嵌套调用,但for循环没有等待,所以我的返回数据都在错误的位置。我正在使用superagent做出反应。

由于

这是我的代码

def server = new ServerSocket(8080)
while (true) {
    server.accept { socket ->
        socket.withStreams { input, output ->
            def reader = input.newReader()
            log.info('Received request:')
            while ((line = reader.readLine()) != null) {
                log.info(line)
            }
        }
    }
}

编辑:问题发生在我正在收集剧集的内部请求上。 var url = import request from 'superagent'; const dataService = request .get('http://my-api.com/api/sets/coll_e8400ca3aebb4f70baf74a81aefd5a78/items/') .end((err, res) => { if (err) { return "error"; } const data = JSON.parse(res.text); let sections = new Array(), section = null, episodes = null; for (var i = 0; i < data.objects.length; i++) { let type = data.objects[i].content_type.toLowerCase(); if(type !== "episode") { if (section !== null) { section.episodes = episodes; sections.push(section); } section = new Object(); episodes = new Array(); section.header = data.objects[i].heading; } if(type === "episode") { var url = `http://my-api.com:8000${data.objects[i].content_url}`; request .get(url) .end((err, res) => { const data2 = JSON.parse(res.text); console.log("data2", data2); var episode = new Object(); episode.title = data2.title; episodes.push(episode); }); } } section.episodes = episodes; sections.push(section); console.log("sections", sections); }) export default dataService ;

1 个答案:

答案 0 :(得分:0)

根据您的描述,我仍然不确定您需要异步的位置。我猜。 但我建议你使用Promise.all()。我将在下面展示一个例子。

import request from 'superagent';
const dataService =

request
        .get('http://my-api.com/api/sets/coll_e8400ca3aebb4f70baf74a81aefd5a78/items/')
        .end((err, res) => {
            if (err) {
                return "error";
            }
            const data = JSON.parse(res.text);

        let sections = new Array(),
            section = null,
            episodes = null;

        Promise.all(for (var i = 0; i < data.objects.length; i++) {
            let type = data.objects[i].content_type.toLowerCase();

            if(type !== "episode") {
                if (section !== null) {
                    section.episodes = episodes;
                    sections.push(section);
                }
                section = new Object();
                episodes = new Array();
                section.header = data.objects[i].heading;
            }

            if(type === "episode") {
                var url = `http://my-api.com:8000${data.objects[i].content_url}`;
                request
                    .get(url)
                    .end((err, res) => {
                    const data2 = JSON.parse(res.text);

                    console.log("data2", data2);
                    var episode = new Object();
                    episode.title = data2.title;
                    episodes.push(episode);
                });
            }
        }).then(() => {
            section.episodes = episodes;
            sections.push(section);

            console.log("sections", sections);
          }


    })

export default dataService

UPDATE -----

Promise.all(if (type === "episode") {
    var url = `http://my-api.com:8000${data.objects[i].content_url}`;
    request
        //with superagent you can use promises like such.
        .get(url)
        .then((err, res) => {
            const data2 = JSON.parse(res.text);

            console.log("data2", data2);
            var episode = new Object();
            episode.title = data2.title;
            episodes.push(episode);
        });
}).then(() => {
    section.episodes = episodes;
    sections.push(section);

    console.log("sections", sections);
})