I am developing a module in BlueMix OpenWHisk, where after a Cloudant feed change, I need to call an url, which would update few details on another platform. I am using nodejs runtime.
The ask is for my action to wait, for the result of the POST request, to the url mentioned above. If the POST was successful, then I should execute the next sequence of events.
Questions:
How to wait for the result of the POST request, before the next sequence is executed ?
Is it possible to wait and return the result of POST request.
Positing my code
/**
*
* main() will be invoked when you Run This Action
*
* @param OpenWhisk actions accept a single parameter, which must be a JSON object.
*
* @return The output of this action, which must be a JSON object.
*
*/
const util = require('util');
var http = require('http');
function main(params) {
// Updated the status of the User
params.status ="updated1";
var options = {
host: "myhost.mybluemix.net",
path: "/addtoimc",
method: "POST",
headers: {
"Content-Type": "text/plain"
}
};
return {message : addtoIMC(options)};
}
function createRequest(data, options)
{
return http.request(options, function (res) {
var responseString = "";
res.on("data", function (data) {
responseString += data;
// save all the data from response
});
res.on("end", function () {
console.log("AAA" + responseString);
});
});
}
function addtoIMC(options)
{
return new Promise(function(resolve, reject) {
var req = createRequest("Hello",options);
var reqBody = "post_data";
req.write(reqBody);
req.end();
});
}