Skip to content

LocalForage usage

Andrew edited this page Dec 11, 2018 · 2 revisions

SETITEM

localforage.setItem('somekey', 'some value').then(function (value) { // Do other things once the value has been saved. console.log(value); }).catch(function(err) { // This code runs if there were any errors console.log(err); });

// Unlike localStorage, you can store non-strings. localforage.setItem('my array', [1, 2, 'three']).then(function(value) { // This will output 1. console.log(value[0]); }).catch(function(err) { // This code runs if there were any errors console.log(err); });

# GETITEM localforage.getItem('somekey').then(function(value) { // This code runs once the value has been loaded // from the offline store. console.log(value); }).catch(function(err) { // This code runs if there were any errors console.log(err); });

ITERATE

// The same code, but using ES6 Promises. localforage.iterate(function(value, key, iterationNumber) { // Resulting key/value pair -- this callback // will be executed for every item in the // database. console.log([key, value]); }).then(function() { console.log('Iteration has completed'); }).catch(function(err) { // This code runs if there were any errors console.log(err); });

REMOVEITEM

localforage.removeItem('somekey').then(function() { // Run this code once the key has been removed. console.log('Key is cleared!'); }).catch(function(err) { // This code runs if there were any errors console.log(err); });