I am trying to do some benchmarking on different style of javascript code, here is what I have:

```
var Promise = require('bluebird');

var timer = function(name) {
    var start = new Date();
    return {
        stop: function() {
            var end  = new Date();
            var time = end.getTime() - start.getTime();
            console.log('Function:', name, 'finished in', time, 'ms');
        }
    };
};

function regular (a, cb) {
    if (a % 2 === 0) return cb(null, a);
    return cb(a);
}

function promise (a) {
    return new Promise (function (resolve, reject) {
        if (a % 2 === 0) resolve(a);
        else reject(a);
    });
}

var t = timer('regular');
var g = 0;
for (var i = 1; i < 10001; i++) {
    regular(i, function(odd, even) {
        if (odd) g = g + (odd * 2);
        else g = g + (even * 3);
    });
}
console.log('g:', g); // g: 125015000
t.stop();

var t2 = timer('promise');
var h = 0;
for (var i = 1; i < 10001; i++) {
    promise(i).then(function(x) {h = h + (x*3);}).catch(function(x) {h = h + (x*2);});
}
console.log('h:', h); // h: 0
t2.stop();

```

What interesting is, the promises doesn't change the global variable 'h', how can I make it return the same result as variable 'g'?

**UPDATE**

Here is changed code that try to get the final result, but the non-deterministic of promises give us unexpected result.

```
for (var i = 1; i < 10001; i++) {
    promise(i).then(function(x) {
        h = h + (x*3); if (x===10000) console.log('h:', h);
    }).catch(function(x) {h = h + (x*2);}); // h: 75015000 
}

```

Currently my code that give the expected output is even stranger.

```
for (var i = 1; i < 10001; i++) {
    promise(i).then(function(x) {
        h = h + (x*3);
    }).catch(function(x) {
        h = h + (x*2);
        if (x===9999) console.log('h:', h);  // <- attention here
    }); // h: 125015000
}

```

Can anyone show me a better code and explain the code above? (The code above shows the deterministic correct result when i is 9999 not 10000)