Fetch Async/Await

by Allie Yu

JavaScript

// Fetch a random joke
function fetchQuote() {
  return fetch( "https://api.icndb.com/jokes/random" ).then(function( resp ){
    return resp.json();
  }).then(function( data ){
    return data.value.joke;
  });
}

async function sayJoke()
{
  try {
    let result = await fetchQuote();
    document.writeln( "Joke:", result );
  } catch( err ) {
    console.error( err );
  }
}
sayJoke();