async/await Example

node v6.14.4
version: 1.2.2
endpointsharetweet
Compares purely Promise-based approach to async/await approach.
Import fetch
const fetch = require("node-fetch");
Promised-based Request
const requestAnimePromise = id => { fetch(`https://jikan.me/api/anime/${id}`) .then(response => response.json()) .then(json => { console.log("Promise-based") console.log(json) }); } requestAnimePromise(1);
async/await based Request
const requestAnimeAwait = async (id=100) => { const response = await fetch(`https://jikan.me/api/anime/${id}`) const json = await response.json(); console.log("async/await based"); console.log(json); } requestAnimeAwait();
Loading…

no comments

    sign in to comment