Node.js:使用来自回调或异步的值调用函数

时间:2016-09-12 05:54:18

标签: node.js asynchronous callback protractor

我在下面写了.js文件来调用下面定义的函数。 objectRepositoryLoader.readObjectRepository()返回一个哈希映射,我必须使用enterUserName(), enterPassword(), clickLoginButton()函数中的值。

var path = require('path');
var elementRepoMap = {}

var LandingPage = function(){
    var fileName = path.basename(module.filename, path.extname(module.filename))

    objectRepositoryLoader.readObjectRepository(fileName+'.xml' , function(elementRepo){
        console.log(elementRepo) //values are being printed here
        this.elementRepoMap = elementRepo
    }); 

    this.enterUserName = function(value){
        console.log(elementRepoMap) //values are not being printed here
        //Some Code     
    };

    this.enterPassword = function(value){
        //Some Code
    };

    this.clickLoginButton = function(){
        //Some Code
    };  
};
module.exports = new LandingPage();

另一个文件中定义的objectRepositoryLoader.readObjectRepository()函数如下:

var ObjectRepositoryLoader = function() {

    this.readObjectRepository = function(fileName, callback) {        
        var filePath = './elementRepository/'+fileName;
        this.loadedMap = this.objectRepoLoader(filePath, function(loadedMap){
            return callback(loadedMap);
        });
    }

    this.objectRepoLoader = function(filePath, callback){
        if (filePath.includes(".xml")) {
            this.xmlObjectRepositoryLoader(filePath, function(loadedMap){
                return callback(loadedMap);                
        });
    }

    this.xmlObjectRepositoryLoader = function (xmlPath, callback){
        var innerMap = {};
        var elementName;
        fs.readFile(xmlPath, "utf-8",function(err, data) {
            if(err){
                console.log('File not found!!')
            }
            else{
                var doc = domparser.parseFromString(data,"text/xml");
                var elements = doc.getElementsByTagName("A1");
                for(var i =0 ; i< elements.length;i++){
                    var elm = elements[i];
                    elementName = elm.getAttribute("name");
                    var params = elm.getElementsByTagName("AS");
                    innerMap = {};
                    for(var j =0 ; j< params.length;j++){
                        var param = params[j];
                        var locatorType = param.getAttribute("type");
                        var locatorValue = param.getAttribute("value");
                        innerMap[locatorType] = locatorValue;
                    }
                    loadedMap[elementName] = innerMap;
                    innerMap={};
                };
            }
            return callback(loadedMap);         
        });        
    };

如何从spec.js文件中调用enterUserName(), enterPassword(), clickLoginButton()函数,有什么方法可以避免使用callback并使用async.js并从spec.js调用enterUserName(), enterPassword(), clickLoginButton()文件?

修改 我修改了我的文件,如下所示:

this.xmlObjectRepositoryLoader = function (xmlPath){
        var innerMap = {};
        var elementName;
        var filePath = xmlPath+'.xml'
        var self = this
        return new Promise(
            function(resolve, reject){
                console.log("In xmlObjectRepositoryLoader : "+filePath)
                self.readFilePromisified(filePath)
                .then(text => {
                    var doc = domparser.parseFromString(text,"text/xml");
                    var elements = doc.getElementsByTagName("Element");
                    for(var i =0 ; i< elements.length;i++){
                        var elm = elements[i];
                        elementName = elm.getAttribute("name");
                        var params = elm.getElementsByTagName("param");
                        innerMap = {};
                        for(var j =0 ; j< params.length;j++){
                            var param = params[j];
                            var locatorType = param.getAttribute("type");
                            var locatorValue = param.getAttribute("value");
                            innerMap[locatorType] = locatorValue;
                        }
                        map[elementName] = innerMap;
                        innerMap={};
                    }
                    console.log(map) // prints the map
                    resolve(text)
                })
                .catch(error => {
                    reject(error)
                });
            });
        }

this.readFilePromisified = function(filename) {
        console.log("In readFilePromisified : "+filename)
        return new Promise(
            function (resolve, reject) {
                fs.readFile(filename, { encoding: 'utf8' },
                (error, data) => {
                    if (error) {
                        reject(error);
                    } else {
                        resolve(data);
                    }
                })
            })
        }

我从另一个文件调用上面的函数,如下所示:

objectRepositoryLoader.readObjectRepository(fileName)
    .then(text => {
        console.log(text);
    })
    .catch(error => {
        console.log(error);
    });

但它给我的错误是

     .then(text => {   ^

TypeError: Cannot read property 'then' of undefined

在这种情况下,如何使用promise调用另一个promise函数,然后在另一个promise函数中使用返回的值,并将计算值返回给调用函数,我可以在其中使用其他函数中的值。我听起来有点困惑。请帮忙

1 个答案:

答案 0 :(得分:0)

您可以使用async.waterfall和async.parallel来执行此任务

请参阅reference

我刚试过你的代码使它工作,我在评论中解释了实现的方式。

async.waterfall([
function(next){
    objectRepositoryLoader.readObjectRepository(fileName+'.xml' ,next)//pass this next as parameter in this function defination and after manipulation return result with callback like this(null,result)

}
  ],function(err,result){

    if(!err){
      //Do wahtever you want with result

async.parallel([
    function(callback){
          this.enterUserName = function(value){
        console.log(elementRepoMap)
        //Some Code     
    };
     },
    function(callback){ 
          this.enterPassword = function(value){
        //Some Code
    };
    },
        function(callback){ 

    this.clickLoginButton = function(){
        //Some Code
    };
        }
], function(err, results) {
    // optional callback
};
    }
  })