Nested promises in node.js
In node.js promises give you return statements and error throwing, which you lose with continuation passing style.
Example:
var createJob = function(jobData){
if(!jobData){
console.log('Job data should not be empty!');
}else{
let jobData = [];
let location = "San Francisco, CA California, US";
let locationArr = location.split(',');
let stateName = locationArr[1].trim();
let country = locationArr[2].trim();
getCountryID(country).then(function(countryID){
jobData['countryID'] = (countryID>0 ? countryID : null);
getStateID(stateName,countryID).then( function (stateID){
jobData['stateID'] = (stateID>0 ? stateID : null);
return jobData;
});
},
function(error) {
console.log(error);
});
}
}
function getCountryID(countryName){
var promise = new Promise(function(resolve, reject) {
//You can write here your logic
resolve( 25 ); //countryID
});
return promise;
}
function getStateID(stateName,countryID){
var promise = new Promise(function(resolve, reject) {
//You can write here your logic
resolve( 25 ); //stateID
});
return promise;
}
Example:
var createJob = function(jobData){
if(!jobData){
console.log('Job data should not be empty!');
}else{
let jobData = [];
let location = "San Francisco, CA California, US";
let locationArr = location.split(',');
let stateName = locationArr[1].trim();
let country = locationArr[2].trim();
getCountryID(country).then(function(countryID){
jobData['countryID'] = (countryID>0 ? countryID : null);
getStateID(stateName,countryID).then( function (stateID){
jobData['stateID'] = (stateID>0 ? stateID : null);
return jobData;
});
},
function(error) {
console.log(error);
});
}
}
function getCountryID(countryName){
var promise = new Promise(function(resolve, reject) {
//You can write here your logic
resolve( 25 ); //countryID
});
return promise;
}
function getStateID(stateName,countryID){
var promise = new Promise(function(resolve, reject) {
//You can write here your logic
resolve( 25 ); //stateID
});
return promise;
}
Comments
Post a Comment