Wednesday, 3 May 2017

stop while loop inside a promise in node js

I am working on a problem in which i have to generate a unique id for user but i am not able to figure it out how to break the while loop inside a Promise.Following function will find if the user with same ref_id exist or not and then return me a resolve promise.

function makeRefId() {

return new Promise(function(resolve , reject){
    var refId = null;
    var flag = true;
    var tempUser ;

    do{
        refId = randomId(); //this will return a random string
        console.log(refId);

        tempUser =   User.findOneAsync({ref_id:refId}).then(function(user){
            if(user == null)
            {
                flag = false;
                resolve(refId);
            }
        });

    }while(flag);

});
}



via Charanjeet Singh

No comments:

Post a Comment