I'm making a POST
request in a POST
request. The second POST
is using a token
from the first POST
request.
I don't really know when the 2nd POST
will finish but I'm pretty sure it will last longer than the expiration time of the token
, let's assume it's about 10 seconds
.
Now my issue is how do I handle it . I mean how do I renew the token knowing that it's used in the 2nd POST
.
Here is my code :
var request = require('request');
var async = require('async');
var fs = require('fs');
var renew_token_timer ;
var o = {
"someValue": "s",
"someValue2": "g"
};
var s = JSON.stringify(o);
request({
headers: {
'Content-Type': 'application/json'
},
uri: '/getToken',
body: s,
method: 'POST'
}, function (err, res, body) {
var renew_token = setInterval(function(){
console.log("renew token now after 10sec");
},10000);
var token = JSON.parse(body);
var tasks = [];
for(let i = 1; i < 10 ; i++){
tasks.push(function(callback){
getPeople(25, i, token, callback);
});
}
async.series(tasks,
function(err, results) {
console.log("FINISHED");
});
});
function getContacts(pageSize, page, access_token, callback){
var to_send = {"test" : "test"};
var to_send = JSON.stringify(to_send);
var url = "/getPeople";
request({
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
time : true,
uri: url,
body: to_send,
method: 'POST'
}, function (err, res, body) {
callback();
});
}
Any advice please ?