Skip to content

Instantly share code, notes, and snippets.

@yosmoc
Created February 23, 2017 21:35
Show Gist options
  • Save yosmoc/bc6b371aab17b3713e2aa0475871224e to your computer and use it in GitHub Desktop.
Save yosmoc/bc6b371aab17b3713e2aa0475871224e to your computer and use it in GitHub Desktop.
const api_endpoint = 'http://petstore.swagger.io/v2/pet/';
function asyncfunc(uri, pet_id) {
let fetch = require('node-fetch');
return fetch(uri, {
method: 'GET',
headers: {'Content-Type': 'application/json'},
body: pet_id
})
}
function parseResponse(response_body_json, input) {
console.log(input);
return new Promise((resolve, reject) => {
resolve(response_body_json);
});
}
// example 1
const pet_id1 = 1;
asyncfunc(api_endpoint, pet_id1)
.then(response => parseResponse(response, pet_id1))
.then(response => {
console.log(response);
}).catch(err => {
console.log(err);
});
// example 2
const pet_id2 = 2
asyncfunc(api_endpoint, pet_id2)
.then(parseResponse.bind(null, pet_id2))
.then(response => {
console.log(response);
}).catch(err => {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment