Skip to content
Advertisement

caching using jquery deferred versus promise

I currently have some code using jQuery Deferred and ajax to call remote api and get the data and put in localStorage and get data from localStorage.

This code seems buggy the first time it runs (i.e. it doesn’t render the data until browser refresh) but it works perfectly after that until the cache expires and same problem again the first time it runs.

I want to rewrite the code using promise to modernize the codebase as well as tackle the bug. I am pretty new to JS promises and I wonder if there is any other improvements that can be made?

JavaScript

//current usage

JavaScript

My new version:

JavaScript

Advertisement

Answer

You could use an async function:

JavaScript

You should no longer use done, but then (or async and await):

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement