Question: mongoosejs async code . 

```
    userSchema.static('alreadyExists',function(name){
    var isPresent;
        this.count({alias : name },function(err,count){
        isPresent = !!count
        });
    console.log('Value of flag '+isPresent);
    return isPresent;
}); 

```

I know isPresent is returned before the this.count async function calls the callback , so its value is undefined . But how do i wait for callback to change value of isPresent and then safely return ?

what effect does 
`(function(){ asynccalls() asynccall() })();` has in the async flow .
What happens if var `foo = asynccall() or (function(){})()` 
Will the above two make return wait ? 

can `process.nextTick()` help?

I know there are lot of questions like these , but nothing explained about problem of returning before async completion

Answer: 
There is no way to do that. You need to change the signature of your function to take a callback rather than returning a value.

Making IO async is one of the main motivation of Node.js, and waiting for an async call to be completed defeats the purpose.

If you give me more context on what you are trying to achieve, I can give you pointers on how to implement it with callbacks.

Edit: You need something like the following:

```
userSchema.static('alreadyExists',function (name, callback) {
    this.count({alias : name}, function (err, count) {
        callback(err, err ? null : !!count);
        console.log('Value of flag ' + !!count);     
    });
});

```

Then, you can use it like:

```
User.alreadyExists('username', function (err, exists) {
    if (err) {
        // Handle error
        return;
    }

    if (exists) {
        // Pick another username.
    } else {
        // Continue with this username.
    }
}

```
Had the same problem. I wanted my mocha tests to run very fast (as they originally did), but at the same time to have a anti-DOS layer present and operational in my app. Running those tests just as they originally worked was crazy fast and ddos module I'm using started to response with Too Many Requests error, making the tests fail. I didn't want to disable it just for test purposes (I actually wanted to have automated tests to verify Too Many Requests cases to be there as well).

I had one place used by all the tests that prepared client for HTTPS requests (filled with proper headers, authenticated, with cookies, etc.). It looked like this more or less:

```
var agent = thiz.getAgent();
thiz.log('preReq for user ' + thiz.username);
thiz.log('preReq for ' + req.url + ' for agent ' + agent.mochaname);

if(thiz.headers) {
    Object.keys(thiz.headers).map(function(header) {
        thiz.log('preReq header ' + header);
        req.set(header, thiz.headers[header]);
    });
}
agent.attachCookies(req);

```

So I wanted to inject there a sleep that would kick in every 5 times this client was requested by a test to perform a request - so the entire suite would run quickly and every 5-th request would wait to make ddos module consider my request unpunishable by Too Many Requests error.

I searched most of the entries here about Async and other libs or practices. All of them required going for callback - which meant I would have to re-write a couple of hundreds of test cases.

Finally I gave up with any elegant solution and fell to the one that worked for me. Which was adding a for loop trying to check status of non-existing file. It caused a operation to be performed long enough I could calibrate it to last for around 6500 ms.

```
for(var i = 0; i < 200000; ++i) {
    try {
        fs.statSync('/path' + i);
    } catch(err) {
    }
};

```