   getWorkflow: (project, workflowID) ->
     # console.log 'Getting workflow', workflowID
     # We could just get the workflow directly, but this way we ensure the workflow belongs to the project.
-    project.get('workflows').then (workflows) ->
-      workflow = (workflow for workflow in workflows when workflow.id is workflowID)[0]
+    project.get('workflows', id: workflowID).then ([workflow]) ->
       unless workflow?
         throw new Error "No workflow #{workflowID} for project #{project.id}"
       workflow

        @setState {projectIsComplete}
        randomIndex = Math.floor Math.random() * workflows.length
        # console.log 'Chose random workflow', workflows[randomIndex].id
        workflows[randomIndex].id

  createNewClassification: (project, workflowID) ->
    workflow = @getWorkflow project, workflowID
    subject = workflow.then (workflow) =>
      @setState {workflow}
      # A subject set is only specified if the workflow is grouped.
      getSubjectSet = if workflow.grouped
        workflow.get('subject_sets').then (subjectSets) =>
          randomIndex = Math.floor Math.random() * subjectSets.length
          subjectSets[randomIndex]
      else
        Promise.resolve()

      getSubjectSet.then (subjectSet) =>
        @getNextSubject project, workflow, subjectSet

    Promise.all([workflow, subject]).then ([workflow, subject]) =>
      # console.log 'Creating a new classification'
      classification = apiClient.type('classifications').create
        annotations: []

    query: 'loadAppropriateClassification'

  componentDidMount: () ->
    @getCurrentWorkflow().then (workflow) =>
      @setState {workflow}

  loadAppropriateClassification: (_, props = @props) ->
    # To load the right classification, we'll need to know which workflow the user expects.
    # console.log 'Loading appropriate classification'
    @promiseToSetState classification: @getCurrentWorkflow(props).then (workflow) =>
      # console.log 'Loading classification for workflow', workflow.id
      # Create a classification if it doesn't exist for the chosen workflow, then resolve our state with it.
      currentClassifications.forWorkflow[workflow.id] ?= @createNewClassification props.project, workflow
      currentClassifications.forWorkflow[workflow.id]

  getCurrentWorkflow: (props = @props) ->
    if props.location.query?.workflow?
      # console.log 'Workflow specified as', props.query.workflow
      # Prefer the workflow specified in the query.
      @getWorkflow props.project, props.location.query.workflow
    else
      # If no workflow is specified, pick a random one and record it for later.
      # When we send this classification, we'll clear this value to select a new random workflow.
      currentWorkflowForProject[props.project.id] ?= @getRandomWorkflow props.project
      currentWorkflowForProject[props.project.id]

  getRandomWorkflow: (project) ->
    project.get('workflows', active: true).then (workflows) =>
      if workflows.length is 0
        throw new Error "No workflows for project #{project.id}"
