Node.js : Call function using value from callback or async

北战南征 提交于 2019-12-25 18:04:03

问题


I have written below .js file to call below defined function. objectRepositoryLoader.readObjectRepository() returns me a hashmap from where i have to use values in enterUserName(), enterPassword(), clickLoginButton() functions.

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();

The objectRepositoryLoader.readObjectRepository() function defined in another file is as below:

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);         
        });        
    };

How can I call enterUserName(), enterPassword(), clickLoginButton() function from spec.js file and is there any way I can avoid using callback and use async.js and call enterUserName(), enterPassword(), clickLoginButton() from spec.js file ?

EDIT I have modified my file like below:

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);
                    }
                })
            })
        }

I am calling above function from another file as below:

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

But it gives me error as

     .then(text => {   ^

TypeError: Cannot read property 'then' of undefined

In this case how can I use promise to call another promise function and then use the returned value in one more promise function and return calculated value to calling function where I can use the value in other functions. I sound a bit confused. Please help


回答1:


You can use async.waterfall and async.parallel to perform this task

see the reference

I just tried your code to make it working, I explained the way of implementation in comment.

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
};
    }
  })


来源:https://stackoverflow.com/questions/39444091/node-js-call-function-using-value-from-callback-or-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!