I am very new to Node family. I have a service which has two endpoint exposed. One is a post method - which takes a payload (which gets processed asynchronously) and sends an acknowledgement to the caller immediately. Another one is a get method - Which is used to check the status of the earlier request.
For example:
Lets assume two end points
(1) http://localhost:8080/myservice/process/11
PayLoad - Any JSON Object
Response: "Request Received. Under Process"
(2) http://localhost:8080/myservice/checkstatus/11
Response: "In-Progress" or "Completed"
From Node module I have to call first end point and then the endpoint will respond with an acknowledgement. Then upon receiving the ack, I need to keep on calling the second GET end point unless it has a response "Completed".
I am not able to understand how can I repeatedly call the endpoint. Any small code snippet will help me understanding the same.
Thanks in advance.
You can use setInterval
for this :
Please Note : Below is a pseudo code and should be used just for reference. Copy pasting would not result in a working solution.
service.post('url-1', payload).then(function(response){
var timer = setInterval(function(){
service.get('url-2').then(function(resp){
if(resp.data == 'Completed') clearInterval(timer);
});
}, 1000);
});
This will poll the service every half second until it gets a response that matches the endingCondition
(whatever you want that to be). Notice that native Promises do not have a 'denodeify' function although its easy enough to write your own or use third-party lib. The done
value is not strictly necessary and could be refactored out if desired, but allows for more introspection. Also note that this is a design smell: as noted in the comments on the question it'd be better to simply return the response from the server when the async process completes.
let request = require('request');
let promise = require('promise');
let done = false;
let poll = promise.denodeify(request.get).bind(request);
let p = poll(serviceURL);
let handle = setInterval(() => {
p.then(res => {
if (res === endingCondition) {
done = true;
clearInterval(handle);
}
if (!done) {
p = poll(serviceURL);
}
});
}, 500);
You can use SynJS for this type of tasks, below is working code:
var SynJS = require('synjs');
var request = require('request');
function requestWrapper(context, url, method, postData) {
console.log('get started:', url, method, postData);
var result = { done: false };
request({
uri: url,
method: method,
postData: postData,
}, function(error, response, body){
result.done = true;
result.body = body;
result.response = response;
console.log('get finished:', url, method, postData);
SynJS.resume(context); // <-indicates that long running functoin is done
});
return result;
}
// synchronous function
function myApiSubmitter(modules) {
var postRes = modules.requestWrapper(_synjsContext, "https://www.google.com/search","POST","asdasd");
SynJS.wait(postRes.done); // <- waits for SynJS.resume, and checks for postRes.done, continues if postRes.done is true
for(var i=0; i<5; i++) {
SynJS.wait(500); // <- waits for 500ms
var getRes = modules.requestWrapper(_synjsContext, "https://www.google.com/","GET",i);
SynJS.wait(getRes.done); // <- waits for SynJS.resume, and checks for getRes.done, continues if getRes.done is true
if(getRes.body.indexOf('some_success_message')>=0) // <- some arbitrary condition to break the cycle
break;
}
};
var modules = {
SynJS: SynJS,
requestWrapper: requestWrapper,
};
SynJS.run(myApiSubmitter,null,modules,function () {
console.log('done');
});
It would produce following output:
get started: https://www.google.com/search POST asdasd
get finished: https://www.google.com/search POST asdasd
get started: https://www.google.com/ GET 0
get finished: https://www.google.com/ GET 0
get started: https://www.google.com/ GET 1
get finished: https://www.google.com/ GET 1
get started: https://www.google.com/ GET 2
get finished: https://www.google.com/ GET 2
get started: https://www.google.com/ GET 3
get finished: https://www.google.com/ GET 3
get started: https://www.google.com/ GET 4
get finished: https://www.google.com/ GET 4
done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.