I am requesting Bitstamp API in parrallel:
// Simplified version
var async = require('async');
var bitstamp = require('bitstamp');
async.parallel([
bitstamp.balance,
bitstamp.ticker
// ...
],
function() (err, result) {
// process results
});
Those two methods are sending signed requests to Bitstamp API including nonce.
Nonce is a regular integer number. It must be increasing with every request you make. Read more about it here. Example: if you set nonce to 1 in your first request, you must set it to at least 2 in your second request. You are not required to start with 1. A common practice is to use unix time for that parameter.
Underlying library generates nonce traditional way:
var nonce = new Date().getTime() + '' + new Date().getMilliseconds();
Problem
Because of asynchronous API calls, sometimes nonce generated it very same millisecond, while remote side wants them increasing.
Question
Keeping parallel requests, any idea to reliably generate sequential nonce?
My obvious try to:
this.nonce = new Date().getTime() + '' + new Date().getMilliseconds();
// ... on request
var nonce = this.nonce++;
But it doesn't solve the problem, same millisecond just increased by one, but still equal.
npm nonce module is now generate it correctly
(author of the npm module here)
I solved it by adding my own counter at the end of the ms timestamp. It now supports up to 999 calls per ms because of this function. The first time it will generate something like
1409074885767000
and if you need a new nonce during the same ms it will then generate1409074885767001
,1409074885767002
, ...I had exactly the same problem, so I took askmike's code and modified it slightly.
use it like this
check out my jsfiddle with example