Zalaboza Zalaboza - 2 months ago 12
Javascript Question

how to use js Promise to eliminate pyramid of doom

i'm trying to understand how to use js Promise api to refractor a code that has lots of nested IF.

example when getting JSON object from localstorage a normal code would look like

function $storage(key,default) {
let json = localStorage.getItem(key);

if(json === null) return default;

try{ // <-- need try catch in case value was not valid json object
json = JSON.parse(json);
} catch (e) {
json = default;
}
return typeof json === 'object' ? json : default;
}


the readibility of this code is not that good. so i thought may be i can utilize js Promise to rewrite it into

function $storage (key, default) {
let ret;

let promise = new Promise( (y,n) => y(localStorage) )
.then( ls => JSON.parse(ls.getItem(key)) )
.then( json => typeof json === 'object' ? json : HOW_TO_THROW_ERROR() )
//on more validation step if needed
.then( json => typeof json === 'object' ? json : HOW_TO_THROW_ERROR() )
.then( valid_json => { return = valid_json } )
.catch( error => { ret = default; console.warn('json invalid',e); } );

return ret;

}


now i want to know how can i throw an exception inside then so that the catch can caught it and execute default ?

is this valid usage of js promise of am i wasting performance

Answer

You could use Promise.reject() to throw an error:

function $storage (key, default) {
 let ret;

 let promise = new Promise( (y,n) => y(localStorage) )
 .then( ls => JSON.parse(ls.getItem(key)) )
 .then( json => typeof json === 'object' ? json : Promise.reject("invalid json") )
 .then( valid_json => { return = valid_json } )
 .catch( err => { ret = default; console.warn(err.message); } );

 return ret;

}

Although I find the following more legible and idiomatic.

function $storage(key,default) {
  let json = localStorage.getItem(key);
  if(json === null ||  typeof json !== 'object') json = default;

  try{  
    json = JSON.parse(json); 
  } catch (e) {
    json = default;
  } finally {
  return json
  }
}

Promises are used, as you surely know, for asynchronous computation. Any other use might confuse other programmers.

Comments