Re: [IndexDB] Proposal for async API changes

On 6/9/2010 3:48 PM, Kris Zyp wrote:
> Another option would be to have cursors essentially implement a JS
> array-like API:
>
> db.objectStore("foo").openCursor(range).forEach(function(object){
>    // do something with each object
> }).onsuccess = function(){
>     // all done
> });
>
> (Or perhaps the cursor with a forEach would be nested inside a
> callback, not sure).
>
> The standard "some" function is also useful if you know you probably
> won't need to iterate through everything
>
> db.objectStore("foo").openCursor(range).some(function(object){
>    return object.name == "John";
> }).onsuccess = function(johnIsInDatabase){
>     if(johnIsInDatabase){
>       ...
>     }
> });
>
> This allows us to have an async interface (the callbacks can be called
> at any time) and still follows normal JS array patterns, for
> programmer convenience (so programmers wouldn't need to iterate over a
> cursor and push the results into another array). I don't think anyone
> would miss getAll() with this design, since cursors would already be
> array-like.
To me, this feels like we are basically doing what we expect a library 
to do: make the syntactic sugar work.  I don't see why a library 
couldn't provide a some or forEach method with the currently proposed API.

Cheers,

Shawn

Received on Wednesday, 9 June 2010 22:52:37 UTC