I have 2 functions:
the first one make an http post to get an xml string
function post(url, formData) {
return new Promise(function (resolve, reject) {
// make an http post and get results (xml string)
request(url, formData, function(error, xml) {
if (error) reject(error)
resolve(xml)
})
})
}
the second transform that xml to an object
function xmlToObject(xml) {
return new Promise( function (resolve, reject) {
// transform xml string to an object using xml2js for example
xml2js(xml, function(error, obj) {
if (error) reject(error)
resolve(obj)
})
})
}
Now I want to call post request and get an xml string and then transform it to an object, so which one is correct and why:
post(url, formData).then( function (xml) {
xmlToObject(xml).then( function (obj) {
// do some work
})
})
Or
post(url, formData).then( function (xml) {
xmlToObject(xml).then( function (obj) {
return obj
})
}).then( function (obj) {
// do some work
})
Or
post(url, formData).then( function (xml) {
return xmlToObject(xml)
}).then( function (obj) {
// do some work
})
Or
post(url, formData).then( xmlToObject )
.then( function (obj) {
// do some work
})