Skip to content

Instantly share code, notes, and snippets.

@bvenkatr
Last active March 31, 2016 06:05
Show Gist options
  • Save bvenkatr/84c87fd00454fb1a474ef17ab07c943c to your computer and use it in GitHub Desktop.
Save bvenkatr/84c87fd00454fb1a474ef17ab07c943c to your computer and use it in GitHub Desktop.
Basic promises using es6 promises
'use strict';
var fs = require('fs');
var request = require('request');
var d = new Promise(function(resolve, reject){
var result = [];
[1,2].forEach(function(item, index){
request.post(
'your_comple_post_url',
{ form: { key: 'value'} },// These are body parameters
function (error, response, body) {
var tempObj = {};
if (!error && response.statusCode == 200) {
result.push(body);
if(index === 1){
resolve(result);
}
} else {
reject(error);
}
});
});
});
d.then(function(data){
console.log(data);
}).catch(function(error){
console.error("Got error ", error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment