Last active
March 31, 2016 06:05
-
-
Save bvenkatr/84c87fd00454fb1a474ef17ab07c943c to your computer and use it in GitHub Desktop.
Basic promises using es6 promises
This file contains 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
'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