<meta name="viewport" content="width=device-width">
<pre id='display-panel'></pre>
<script id="jsbin-javascript">
function getRandomInt(min, max) {
return Math.random() * (max - min) + min;
const search = (word, callback) => {
const results = ['foo', 'bar', 'baz', 'baz2', 'etc', 'etc2'];
const numberOfResults = getRandomInt(1, results.length);
const randomTimeout = getRandomInt(1, 4);
callback(null, results.slice(0, numberOfResults));
const display = (data) => {
console.log('display', data);
const el = document.querySelector('#display-panel');
const msg = (typeof data === 'string') ? data : data.join(', ');
const displaySearchResults = (error, results) => {
display('Search failed, please retry later.');
// will display random results each time
// search(word, displaySearchResults);
const searchWrapper = (word, callback) => {
const key = `search-${word}`;
const results = sessionStorage.getItem(key);
// keeping things asynchronous
setTimeout(() => callback(null, results), 1);
search(word, (error, res) => {
if (error) { return callback(error); }
// keeping the results in cache
sessionStorage.setItem(key, res);
// will always display the same results because they are cached
searchWrapper('cats', displaySearchResults);
<script id="jsbin-source-javascript" type="text/javascript">function getRandomInt(min, max) {
return Math.random() * (max - min) + min;
const search = (word, callback) => {
const results = ['foo', 'bar', 'baz', 'baz2', 'etc', 'etc2'];
const numberOfResults = getRandomInt(1, results.length);
const randomTimeout = getRandomInt(1, 4);
callback(null, results.slice(0, numberOfResults));
const display = (data) => {
console.log('display', data);
const el = document.querySelector('#display-panel');
const msg = (typeof data === 'string') ? data : data.join(', ');
const displaySearchResults = (error, results) => {
display('Search failed, please retry later.');
// will display random results each time
// search(word, displaySearchResults);
const searchWrapper = (word, callback) => {
const key = `search-${word}`;
const results = sessionStorage.getItem(key);
// keeping things asynchronous
setTimeout(() => callback(null, results), 1);
search(word, (error, res) => {
if (error) { return callback(error); }
// keeping the results in cache
sessionStorage.setItem(key, res);
// will always display the same results because they are cached
searchWrapper('cats', displaySearchResults);