Node Js表示请求和Redis调用的异步响应

时间:2016-09-09 20:56:36

标签: javascript node.js asynchronous redis node-async

所以我在尝试向请求发送响应时遇到了在NodeJS中处理异步操作的问题,中间有一些异步调用。 (为了使这个派对更加复杂,我也想使用async.parallel)

基本上我试图从Redis获得价值,如果他不存在,请从提供商处获取(使用axios进行请求和响应)。

这是代码段:

this.getFixturesByTimeFrame = function (timeFrame, res) {
    function getGamesData(timeFrame,finalCallback) {
        var calls = [];
        var readyList = [];

        //Creating calls for parallel
        timeFrame.forEach(function(startDay){
            calls.push(function(callback) {
                //Problematic async call
                redisClient.get(startDay, function (error, exist) {
                    console.log('Got into the redis get!');

                    if (error){
                        console.log('Redis error : '+error);
                    }

                    if (exist) {
                        console.log('Date is in the cache! return it');
                        return exist;
                    } else {
                        //We need to fetch the data from the provider
                        console.log('Date is not in the cache, get it from the provider');
                        getFixturesDataFromProvider(startDay)
                            .then(organizeByLeagues)
                            .then(function (gamesForADay) {
                                redisClient.setex(startDay, 600, gamesForADay);
                                responsesList.add(gamesForADay);
                                callback(null, gamesForADay);
                            }).catch(function (response) {
                            if (response.status == 404) {
                                callback('Cant get games from provider');
                            }
                        });
                    }


                });
                }
            )});

        async.parallel(calls, function(err, responsesList) {
            /* this code will run after all calls finished the job or
             when any of the calls passes an error */
            if (err){
                res.send(501);
            } else {
                console.log('Here is the final call, return the list here');
                //Some data manipulation here - just some list ordering and for each loops
                console.log('finished listing, send the list');
                finalCallback(responsesList);
            }
        });


    }

    getGamesData(timeFrame, function (readyList) {
        res.send(readyList);
    });
};

function getFixturesDataFromProvider(date) {
    var requestUrl = 'someURL/ + date;
    return axios.get(requestUrl, config);
}

function organizeByLeagues(matchDay) {
    if (matchDay.count == 0) {
        console.log('No games in this day from the provider');
        return [];
    } else {
        var organizedSet = [];
       //Some list manipulations using for each 
        return organizedSet;
    }
}

但响应是在并行开始做他的事情之前发送的......

我确实错过了回调和异步电话,但我不知道在哪里......

由于

0 个答案:

没有答案