Last active
January 28, 2018 00:25
-
-
Save ruhnet/587ee87e706db211299284c7659dbaf6 to your computer and use it in GitHub Desktop.
Promise.all What's wrong with this?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function processAllObjects(bunchOfObjects) { | |
return Promise.all(bunchOfObjects.map(function (object, i) { //for each object | |
if (object.testProperty) { | |
console.log('OBJECT #'+i+': '+object.Id+' IS VALID! Checking...'); | |
return Promise.try(function() { | |
return checkLoadDB(object); //check if exists in DB, mark it. | |
}) | |
.then(function(object) { | |
console.log(' Looking up '+object.Id+'...'); | |
return getObjectDetails(object); //get more details on object and add them. | |
}) | |
.then(function(object) { | |
if (!object.storedInDB) { | |
return processObject(object); //put the object through tests and modify it. | |
} else { | |
return object; | |
} | |
}) | |
.then(function(object) { | |
if (!object.storedInDB) { | |
object.storedInDB = true; | |
return dbObjectInsert(object); //store object load in db | |
} else { | |
return false; | |
} | |
}) | |
.catch(function(err) { | |
console.log(''); | |
console.error('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); | |
console.error('ERROR Processing Objects (inside Promise.all loop)!'); | |
console.error(JSON.stringify(err)); | |
console.error('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); | |
console.log(''); | |
}); | |
} | |
}) | |
.catch(function(err) { | |
console.log(''); | |
console.error('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); | |
console.error('ERROR Processing All Objects!'); | |
console.error(JSON.stringify(err); | |
console.error('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); | |
console.log(''); | |
}); | |
} | |
function getObjectDetails(shipment) { | |
return agent.get(loadDetailUrl+shipment.movementId) //get details of this shipment from server. | |
.redirects(2) | |
.retry(2) | |
.set('User-Agent', uaString) | |
.then(function (response) { | |
console.log("_-_-_-_-_-_-_-_-_MESSING w OBJECT "+shipment.movementId+"details HERE _-_-_-_-_-_-_-_"); | |
let $ = cheerio.load(response.text); | |
console.log('---------------------------------------------------'); | |
shipment.match = false; | |
console.log('Shipment Distance is '+shipment.totalPracticalMiles+' Miles' ); | |
console.log('Pickup Destination: '+shipment.originStop.cityNm+' '+shipment.originStop.stateCd+' '+shipment.originStop.postalNbr ); | |
console.log('Delivery Destination: '+shipment.destinationStop.cityNm+' '+shipment.destinationStop.stateCd+' '+shipment.destinationStop.postalNbr ); | |
shipment.dhpuMiles = $('#dhpuMiles').text(); | |
console.log('Miles: '+$('#dhpuMiles').text()); | |
console.log("_-_-_-_-_-_-_-_-_DONE MESSING w OBJECT "+shipment.movementId+"details HERE _-_-_-_-_-_-_-_"); | |
return shipment; | |
}) | |
.catch(function(err) { | |
console.log(''); | |
console.error('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); | |
console.error('ERROR getting load details for shipment '+shipment.movementId); | |
console.error(err.message); | |
console.error('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); | |
console.log(''); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to pass individual objects from bunchOfObjects through the functions that are called in the .then blocks of the Promise.try on line 5. Each of the functions returns a promise which resolves to the object after it has been modified by the functions. For example, the checkLoadDB() function takes the object as input, checks if it's in the database, and if it is, adds a property object.storedInDB = true and then returns the object (or the promise, rather).
But as the object gets sent down through the chain, it gets mixed up with other attributes from the other objects being sent through the chain by Promise.all() somehow. I think stuff is being called out of order.
Is there anything obviously wrong with this code that i have?