SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 112 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fetch = require('node-fetch');
  2.  
  3. const defaultURL = 'https://api.randomuser.me/';
  4.  
  5.  
  6. // request -- startline, headers, and body
  7. const getRequestInit = {
  8.     method: 'GET', // *GET, POST, PUT, DELETE, etc.
  9.     mode: 'cors', // no-cors, cors, *same-origin
  10.     cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
  11.     credentials: 'same-origin', // include, *same-origin, omit
  12.     headers: {
  13.         'Content-Type': 'application/json',
  14.         // 'Content-Type': 'application/x-www-form-urlencoded',
  15.     },
  16.     redirect: 'follow', // manual, *follow, error
  17.     referrer: 'no-referrer', // no-referrer, *client
  18.     //body: JSON.stringify(data), // body data type must match "Content-Type" header
  19. }
  20.  
  21. const handleErrors = (response) => {
  22.     // Can handle response.status codes, but this is very basic implimentation
  23.     if (!response.ok) throw Error(response.status);
  24.     return response;
  25. }
  26.  
  27. // First implimentation with promises. -- Least favorite
  28. function getData(url = defaultURL) {
  29.  
  30.     // Fetch data with url parameter, and request info.
  31.     return fetch(url, getRequestInit)
  32.         .then(handleErrors)
  33.         .then( (response) => {
  34.             return response.json();
  35.         })
  36.         .catch( (error) => {
  37.             console.log("Error fetching random users: ", error.message);
  38.         });
  39. };
  40.  
  41.  
  42. // Async and Await implimentation -- My favorite
  43. const getDataASync = async (url = defaultURL) => {
  44.     try {
  45.         const response = await fetch(url, getRequestInit);
  46.         const { results } = await response.json();
  47.  
  48.         return results;
  49.     } catch(error) {
  50.         //Do magic error stuff
  51.         console.log("Error fetching random users: ", error.message);
  52.     }
  53. };
  54.  
  55. // Async and Await implimentation -- Second favorite
  56. const getDataAsyncTwo = async (url = defaultURL) => {
  57.         return await fetch(url, getRequestInit)
  58.         .then(handleErrors)
  59.         .then( async (response) => {
  60.             let { results } = await response.json();
  61.             return results;
  62.         })
  63.         .catch((error) => {
  64.             console.log("Error fetching random users: ", error.message);
  65.         })
  66. };
  67.  
  68. // Execute fetching arbritrary user data
  69. //getData().then( (data) => console.log(data.results) );
  70. getDataASync().then((data) => console.log(data));
  71. //getDataAsyncTwo().then( (data) => console.log(data));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top