Please keep in mind that I am new to node.js and I am used with android development.
My scenario is like this:
- Run a query against the database that returns either null or a value
- Call a web service with that database value, that offers info paginated, meaning that on a call I get a parameter to pass for the next call if there is more info to fetch.
- After all the items are retrieved, store them in a database table
- If everything is well, for each item received previously, I need to make another web call and store the retrieved info in another table
- if fetching any of the data set fails, all data must be reverted from the database
So far, I've tried this:
getAllData: function(){
self.getMainWebData(null)
.then(function(result){
//get secondary data for each result row and insert it into database
}
}
getMainWebData: function(nextPage){
return new Promise(function(resolve, reject) {
module.getWebData(nextPage, function(errorReturned, response, values) {
if (errorReturned) {
reject(errorReturned);
}
nextPage = response.nextPageValue;
resolve(values);
})
}).then(function(result) {
//here I need to insert the returned values in database
//there's a new page, so fetch the next set of data
if (nextPage) {
//call again getMainWebData?
self.getMainWebData(nextPage)
}
})
There are a few things missing, from what I've tested, getAllData.then fires only one for the first set of items and not for others, so clearly handling the returned data in not right.
LATER EDIT: I've edited the scenario. Given some more research my feeling is that I could use a chain or .then()
to perform the operations in a sequence.
t.sequence(index
? For instance, on each web call, I get the nextPage value but I don't understand how you actually callgetNextData(index)
where I don't seeindex
being save or sent anywhere since it may change on each getNextData or be null. – InconsonantnextPage
is independent from the actual result, how do I pass it foward fromgetNextData
? – Inconsonantindex
increments, I want to have the nextPage value there which is not 0,1,2... it could be a String for instance. – Inconsonant.then(data => {...inserting}).then(()=>{return whatever-string});
, then use it asgetNextData(pageIndex, previousString){}
. – Larcher