Question: I'm writing tests for my Node.js/Express/Mongoose project using Mocha and Should.js, and I'm testing out my functions that access my MongoDB. I'm want these tests to be completely independent from the actual records in my database, so I want to create an entry and then load it, and do all my tests on it, then delete it. I have my actual functions written (I'm writing tests after the entire project is complete) such that the `create` function does not have a callback; it simply just renders a page when it's done. In my tests script, I call my `load_entry` function after I call `create`, but sometimes `create` takes longer than usual and thus `load_entry` throws an error when it cannot actually load the article since it has yet to be created. Is there any way to make sure an asynchronous function is finished without using callbacks? 

Please let me know if there is any more info I can provide. I looked all over Google and couldn't find anything that really answered my question, since most solutions just say "use a callback!"

Answer: 
Use what is known as a `promise`

You can read more about it **[here](http://blogs.msdn.com/b/ie/archive/2011/09/11/asynchronous-programming-in-javascript-with-promises.aspx)**.

There are lots of great libraries that can do this for you.

**[Q.js](https://github.com/kriskowal/q)** is one I personally like and it's widely used nowadays. Promises also exist in **[jQuery](http://api.jquery.com/promise/)** among many others.

Here's an example of using a q promise with an asynchronous `json-p` call: **[DEMO](http://jsfiddle.net/ucB4G/1/)**

```
var time;
$.ajax({
    dataType: 'jsonp',
    type: 'GET',
    url: "http://www.timeapi.org/utc/now.json",
    success: function (data) {
        time = data;
    },
    error: function (data) {
        console.log("failed");
    }
})
.then(function(){ // use a promise library to make sure we synchronize off the jsonp
    console.log(time);    
});

```
Since the only native way to do asynchronous things are: `setTimeout`, `setInterval` and `addEventListener`, and they all take a callback you will eventually have to use a callback somewhere.

However, you can hide that by using Promises/A, also known as Deferreds.

Your code could look like this:

```
db.create_entry("foo", data).done(function (entry) {
    db.delete_entry(entry).done(function () {
        console.log("entry is deleted");
    });
});

```

Using then-chaining:

```
db.create_entry("foo", data).then(function (entry) {
    return db.delete_entry(entry);
}).done(function () {
    console.log("entry is deleted");
});;

```