import map from 'lodash/collection/map'
import getCourse from './get-course'

/**
 * Gets a list of course ids from the database.
 *
 * @param {Array} clbids - a list of class/lab IDs
 * @returns {Promise[]} - an array of course promises
 * @fulfill {Array} - the courses.
 */
export default function getCourses(clbids, {year, semester}={}) {
	// console.log('called getCourses', clbids)

	// Takes a list of clbids, and returns a list of the course objects for
	// those clbids.

	//
	// Current problem: this rejects as soon as one item is rejected.
	// I want an equivalent that waits for *all* items to be either resolved or rejected,
	//   and returns an array of the results or rejections.
	//

	return Promise.all(map(clbids, getCourse))
		.then(courses => map(courses, course => {
			if (!course || course.error) {
				// embed the year and semester that it came from in the case of an error
				return {...course, year, semester}
			}
			return course
		}))
}

@@ -340,54 +340,8 @@
 ) %7B%0A
-%09// console.log('called getCourses', clbids)%0A%0A
 %09// 
@@ -646,16 +646,71 @@
 ons.%0A%09//
+%0A%09// Solution (probably): Bluebird's Promise.settle%0A%09//
 %0A%0A%09retur

add note to getCourse