I know it is a simple, dumb question but it's been two days since I'm stuck on it.
Consider there is a function, managing creation of object type Course
from some object type UnsafeCourse
, like so:
Class Schedule {
constructor() {
this.courses = [];
}
sectionNeedsCourse(unsafeCourse) {
this.courses.forEach(function (course, index, array) {
if (course.couldBeRepresentedBy(unsafeCourse)) {
return course;
}
}
return new Course(unsafeCourse, [some additional parameters...]);
}
}
As things in node.js works asynchronously, for loop is gone-through.
I tried different approaches with Promise
s, didn't work.
sectionNeedsCourse(unsafeCourse) { //-> Promise
return new Promise (function (resolve) {
new Promise(function (resolve) {
this.courses.forEach(function (course, index, array) {
if (course.couldBeRepresentedBy(unsafeCourse)) {
resolve(eachCourse);
}
});
resolve(null);
}).then(function (result) {
console.log(result);
if (result != null) {
addCourse(unsafeCourse).then(function (result) {
resolve(result);
});
} else {
resolve(result);
}
});
});
}
Also, is it a good practice to use multiple Promise
s in same function?
Does it make heavy overhead?