async.js loop around the async call

I'm generate a unique id before the model is saved to the database. So I'm calling a method to generate a random unique id and then checking if that id is available. Here's the while loop I'm using but it never breaks out of the while loop:

beforeValidation: function(values, next) {

var counter = 0;
var slug = SlugService.generate_slug();

console.log(counter + ":" + slug);
async.whilst(
  function () { return values.slug === undefined && counter <= 10; },
  function (callback) {
    counter++;
    Lists.findOne({slug: slug}).exec(function(err, list) {
      if (list === undefined) values.slug = slug;

      slug = SlugService.generate_slug();
      console.log(counter + ":" + slug);
    });
  },
  function (err) {
    console.log("err");
    values.slug = slug;
  }
  );

next();

}

I'm using async library but it doesn't seem to be working. It just calls next(); before the whilst loop is done.

I also created a Service class and added this method there but this doesnt seem to work either:

function checkSlug(method, counter) {
  slug = SlugService.generate_slug();
  method({ slug: slug }).exec(function(err, list) {
    if (list !== undefined && counter <= 10) {
      counter++;
      checkSlug();
    }
  });
}

I would prefer to get the option # 2 working so I can share this across all my models.

you have different mistakes in your async-function.

1.) You dont have a callback in your function (see: https://github.com/caolan/async#whilst) 2.) You called the sails callback before async finished

try this:

beforeValidation: function(values, next) {

  var slug = "";

  async.whilst(
    function check_slug() { return values.slug === undefined },
    function generate_slug(callback) {

      slug = SlugService.generate_slug();

      Lists.findOne({slug: slug}).exec(function(err, result) {
        if (err) {
          return callback(err);
        }

        if (result === undefined){
          values.slug = slug;
        } 

        callback();
    });
  },
  function (err) {
    if(err){
      return console.log(err);
    }

    next();
  });
}