0
function getNextID(number){
var maxKey = 0;
number = typeof number !== 'undefined' ? number : 1;

db.from('contributions').select('id').list().done(function(records) {

records.forEach(function(item){
    if (maxKey < item){
        maxKey = item;
    }
});

return(maxKey);

});

}

I am using ydn-db

This is a class for working with local storage - it works asynchronously, not AJAX!

What I am trying to do is get the next ID on a table (but that bit is the irrelevant part really).

The problem I am having is either:

a) I am an idiot and getting my scope wrong for maxKey

b) the asynchronous calls are messing everything up.

At the moment the function always returns 'undefined' - however if I replace return with console.log(maxKey); it works fine.

Can anyone tell me how I can fix this function so that it can be called correctly?

(it is an example function so although any built-in function for finding the next key would be appreciated I really need to know how to return asynchronous items from within a function!)

I hope that is clear - any questions - fire away!

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64
  • 1
    Please read [How to return the response from an AJAX call?](http://stackoverflow.com/q/14220321/218196). The problem is b and you have to return a promise from `getNextID`. – Felix Kling Mar 19 '14 at 17:08
  • Hi Felix - it isn't AJAX it is local storage - I will ammend my question for clarity! – GrahamTheDev Mar 19 '14 at 17:09
  • 1
    It doesn't matter. Asynchronous is asynchronous and callbacks, promises are the solution for every asynchronous function. – Felix Kling Mar 19 '14 at 17:09
  • Thanks Felix - you are correct I just hadn't got the point you were trying to make! – GrahamTheDev Mar 19 '14 at 17:19
  • Yeah, it's not obvious from the title that this is a solution that applies to every asynchronous situation. I considered changing the question/answer to make it more generic, but didn't find the time yet. – Felix Kling Mar 19 '14 at 17:20

2 Answers2

2
function getNextID(number,callback){
var maxKey = 0;
number = typeof number !== 'undefined' ? number : 1;

db.from('contributions').select('id').list().done(function(records) {

records.forEach(function(item){
    if (maxKey < item){
        maxKey = item;
    }
});

callback(maxKey);

});

}

then in your code

getNextID(number,function(maxKey){
   // continue the code here.
});

since your code is async you cant return anything from getNextID but a promise or use a continuation (a callback).

mpm
  • 20,148
  • 7
  • 50
  • 55
  • THANK YOU! i am such an idiot - this promise stuff always catches me out! Callback from a function with a callback was obviously too much for my ickle brain to handle! Can't accept yet but will when i can. – GrahamTheDev Mar 19 '14 at 17:14
1

Since primary key are sorted, you can get the maximum key just by taking the first one:

function getNextID(number,callback){

var key_range = number ? ydn.db.KeyRange.lowerBound(number, true) : null;

var reverse = true; // sort by descending order
var iter = new ydn.db.KeyIterator('contributions', key_range, reverse);

db.get(iter).done(function(maxKey) {

  callback(maxKey);

});

}
Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • thanks kyaw tun - your library (it is yours or you are involved right?) is great, although I am probably brute forcing a lot of functionality that is baked in as I am a bit of a noob and have never worked with indexed db etc. so am learning all of this from scratch! – GrahamTheDev Mar 20 '14 at 10:26