Last active
February 3, 2016 23:20
-
-
Save awilson28/b31efec06a85ee0721be to your computer and use it in GitHub Desktop.
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 _promiseForAssessmentPastDue (assessment, studentSolution, stepId) { | |
// use Promise resolve so that we can .spread later on (.spread is not a method on the mongoose object) | |
return Promise.resolve(mongoose.model('User').findById(studentSolution.user)) | |
.then(_user => { | |
return Promise.all([ | |
mongoose.model('CohortAssessment').findOne({ assessment: assessment._id, cohort: _user.cohort }), | |
mongoose.model('Solution').find({ user: studentSolution.user, step: stepId }) | |
]) | |
}) | |
.spread(function(cohortAssessment, gitHubSolution) { | |
let now = Date.now(); | |
return (_pastEndDate(now, cohortAssessment.end) || _pastDuration(gitHubSolution.start, now, cohortAssessment.duration)); | |
}); | |
} | |
// to be clear, here's how we use bind: | |
function _promiseForAssessmentPastDue (assessment, studentSolution, stepId) { | |
// use Promise resolve so that we can .spread later on (.spread is not a method on the mongoose object) | |
return Promise.resolve(mongoose.model('User').findById(studentSolution.user)).bind(this) | |
.then(_user => { | |
// attach info to "this", which is the promise object and can be accessed within any subsequent "then" | |
this.cohort = _user.cohort; | |
return Promise.all([ | |
mongoose.model('CohortAssessment').findOne({ assessment: assessment._id, cohort: _user.cohort }), | |
mongoose.model('Solution').find({ user: studentSolution.user, step: stepId }) | |
]) | |
}) | |
.spread(function(cohortAssessment, gitHubSolution) { | |
// we can access this.cohort down here | |
//typeof this.cohort === 'object' // true | |
let now = Date.now(); | |
return (_pastEndDate(now, cohortAssessment.end) || _pastDuration(gitHubSolution.start, now, cohortAssessment.duration)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment