This function fetches response and writes response to localStorage
function getSomething() {
var d = $.Deferred();
var payload = ...
$.post(API, payload).done(function (data) {
// addData is a wrapper for localStorage write
addData(payload);
d.resolve();
}).fail(d.reject);
return $.when(d).done(function () {
console.log('API fetch OK');
}).promise();
}
And then on document.ready the following executes
getSomething().done(function () {
// getData is a wrapper for localStorage read
var fs = $.grep(getData(), function (i) {
return i.cid == getID();
});
var fso = JSON.parse(fs[0].data);
$('.button-list').each(function (i, v) {
var buttonRow = '';
for (i = 0 ; i < fso.length ; i++) {
buttonRow += '<a id=' + getID() + '-' + fso[i].Content.Id + ' class=\'btn btn-default\'>' + fso[i].Form.Name + '</a>';
}
$(v).html(buttonRow);
});
So the problem here is I'd like to add another function between the two which checks if something is already present in localStorage before calling getSomething(). This fails because getSomething is 'done' before localStorage is written to. After some fiddling around I found localStorage is available to read 200ms after getSomething is 'done'.
My question - is it possible to guarantee localStorage has been written to before getSomething enters 'done' state?
Thanks!