Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

getting data from an api

function sayJoke(apiUrl, jokeId) {
    return fetch(apiUrl).then(response =>
        response.json()
    ).then(json => {
        for (let joke of json.jokes) {
            if (joke.id === jokeId) {
                return {saySetup: _ => joke.setup, sayPunchLine: _ => joke.punchLine};
            }
        }
        throw new Error(`No jokes found id: ${jokeId}`);
    }).catch(error => {
        throw (error instanceof TypeError)? new Error(`No jokes at url: ${apiUrl}`) : error
    });
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript divide string into two parts 
Javascript :: create-react-app npm yarn 
Javascript :: electron load index.html 
Javascript :: stateless vs stateful components in react 
Javascript :: js clearect 
Javascript :: node js do request 
Javascript :: dynamically add/remove rows in html table using javascript 
Javascript :: p5js left mouse click 
Javascript :: vue js app component 
Javascript :: framer motion reactjs 
Javascript :: js get element by innertext 
Javascript :: serve static files from express 
Javascript :: moment is today 
Javascript :: urlsearchparams to object 
Javascript :: react router dom v6 private route 
Javascript :: function countdown() 21 sec 
Javascript :: js setinterval vs settimeout 
Javascript :: javascript remove first element from array 
Javascript :: what is redis 
Javascript :: convert multidimensional array to string javascript 
Javascript :: array javascript 
Javascript :: sub array javascript 
Javascript :: find if two elements in array sum to given integer js 
Javascript :: javascript slice string from character 
Javascript :: moment.set 
Javascript :: date.gettime is not a function 
Javascript :: first letter string uppercase javascript 
Javascript :: react navigation hide header 
Javascript :: try catch with for loop in javascript 
Javascript :: mongodb mongoose update convert string to object 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =