async.series函数未完成

时间:2014-09-29 18:16:45

标签: node.js asynchronous sails.js waterline async.js

async.js我遇到了一个奇怪的问题。我正在解释整个场景。我在数据库中有两个表。设一个X为5行,另一个为Y,共2840行。由于我的项目在sails.js,我使用waterline作为ORM。我正在async.series中运行三个函数。第一个用于从表X中获取所有行,第二个用于从表Y中获取所有行。在第三个函数中,我在async.eachSeries的所有行上运行Y,这些行在第二个函数中获取并调用外部POST api。所以整体流程是,

async.series([
    function( callback1 ){
        // Fetch all rows from `X`
    },
    function( callback1 ){
        // Fetch all rows from `Y`
    },
    function( callback1 ){
        async.eachSeries(allRowsOfY, function(item, callback2){
            // Call an external POST api.
        },
        function(err){

        });
    },
], 
function( err ){
    // Do something
});

问题是,即使Y有2840行,也只有大约600个api调用,然后async.series开始从第一个开始运行。这件事一直在继续。为什么会这样?

1 个答案:

答案 0 :(得分:1)

当您拥有所有需要的数据时,将async.series移至回调部分

#include <baseapi.h>
#include <allheaders.h>
#include <iostream>
#include "D:\\std_lib_facilities.h"

int main()
{
    string outText="";
        do {
            system("screenshot.exe -rt 556 145 674 171");
            tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
        // Initialize tesseract-ocr with English, without specifying        tessdata path
        if (api->Init(NULL, "eng")) {
            fprintf(stderr, "Could not initialize tesseract.\n");
            exit(1);
        }
        // Open input image with leptonica library
        Pix *image = pixRead("screenshot.png");
        api->SetImage(image);
        // Get OCR result
        outText = api->GetUTF8Text();
        cout << outText;
        Sleep(1000);
        //Cant get this to work how I expect ... it thinks outtext not equal the value even when it is read
        //and outputed correctly in the console
    } while (outText != "Welcome");
    //rest of code
    cout << "zezooooooooooooooo";
}