Skip to content

Instantly share code, notes, and snippets.

@GuyMograbi
Last active June 23, 2017 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuyMograbi/fed87b447543a6eb9786ae5fb3c7945c to your computer and use it in GitHub Desktop.
Save GuyMograbi/fed87b447543a6eb9786ae5fb3c7945c to your computer and use it in GitHub Desktop.
improve javascript stacktrace
function getName () {
return new Promise((resolve)=>{
resolve({status:500});
}).then((response) => { // validate output
if (response.status === 500){
throw new Error('request to get name has failed');
}else{
return response;
}
}); // emulate http call
}
function getFullName (info) {
if (!info){ // validate input
throw new Error('info is missing');
}
return new Promise((resolve) => {
resolve(info.firstName + ' ' + info.lastName)
})
}
function countLetter(fullName, letter){
if (!fullName || !letter){ // validate input
throw new Error('letter or fullName is undefined fullName=[' + fullName + '] letter=[' + letter + ']');
}
return new Promise((resolve)=>{
resolve(fullName.split(letter).length-1);
})
}
function countLetters (fullName) {
if (!fullName){ // validate input
throw new Error('fullName is missing');
}
const result = {};
return Promise.all('abcdefghijklmnopqrstuvwxyz'.split('').map((char)=>{
countLetter(fullName, char).then((count)=>{
result[char] = count;
})
})).then(()=>result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment