Last active
April 29, 2020 08:02
-
-
Save gnilchee/eeb92fa4b3388f7c76f43d9a70bac2e2 to your computer and use it in GitHub Desktop.
Using node-fetch and Promises. Comparing using a function vs within the promise to manipulate the data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import node-fetch module | |
const fetch = require("node-fetch"); | |
// return user-agent from request | |
const userAgent = fetch("https://httpbin.org/user-agent") | |
.then((response) => response.json()) | |
.then((response) => response); | |
// return origin IP from request | |
const originIP = fetch("https://httpbin.org/ip") | |
.then((response) => response.json()) | |
.then((response) => response); | |
// return all headers from request with custom user-agent | |
const headers = fetch("https://httpbin.org/headers", { | |
headers: { "User-Agent": "gregrequest/1.0" }, | |
}) | |
.then((response) => response.json()) | |
.then((response) => response); | |
// gather all promises and do work | |
Promise.all([userAgent, originIP, headers]) | |
.then((responses) => { | |
let my_object = {}; | |
for (let resp of responses) { | |
for (let key of Object.keys(resp)) { | |
my_object[key] = resp[key]; | |
} | |
} | |
return my_object; | |
}) | |
.then((result) => console.log(JSON.stringify(result, null, 4))) | |
.catch(() => console.log("One of the APIs returned an error")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import node-fetch module | |
const fetch = require("node-fetch"); | |
// return user-agent from request | |
const userAgent = fetch("https://httpbin.org/user-agent") | |
.then((response) => response.json()) | |
.then((response) => response); | |
// return origin IP from request | |
const originIP = fetch("https://httpbin.org/ip") | |
.then((response) => response.json()) | |
.then((response) => response); | |
// return all headers from request | |
const headers = fetch("https://httpbin.org/headers", { | |
headers: { "User-Agent": "gregrequest/1.0" }, | |
}) | |
.then((response) => response.json()) | |
.then((response) => response); | |
// function to gather responses and return an object | |
function gatherResp(responses) { | |
let my_object = {}; | |
for (let resp of responses) { | |
for (let key of Object.keys(resp)) { | |
my_object[key] = resp[key]; | |
} | |
} | |
return my_object; | |
} | |
// using above function gather all promises and do work | |
Promise.all([userAgent, originIP, headers]) | |
.then(gatherResp) | |
.then((result) => console.log(JSON.stringify(result, null, 4))) | |
.catch(() => console.log("One of the APIs returned an error")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multiple_promises.js | |
-------------------- | |
# node multiple_promises.js | |
{ | |
"user-agent": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)", | |
"origin": "_requestor_ip_", | |
"headers": { | |
"Accept": "*/*", | |
"Accept-Encoding": "gzip,deflate", | |
"Host": "httpbin.org", | |
"User-Agent": "gregrequest/1.0", | |
"X-Amzn-Trace-Id": "_trace_id_" | |
} | |
} | |
multiple_promises_func.js | |
------------------------- | |
# node multiple_promises_func.js | |
{ | |
"user-agent": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)", | |
"origin": "_requestor_ip_", | |
"headers": { | |
"Accept": "*/*", | |
"Accept-Encoding": "gzip,deflate", | |
"Host": "httpbin.org", | |
"User-Agent": "gregrequest/1.0", | |
"X-Amzn-Trace-Id": "_trace_id_" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment