0

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 Promises, 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 Promises in same function?

Does it make heavy overhead?

Buğra Ekuklu
  • 3,049
  • 2
  • 17
  • 28
  • Wait, you're `return`ing from within a `.forEach` callback? Looks like even your original code doesn't work like one might expect. What is exactly is *supposed* to happen? – Bergi Oct 15 '15 at 18:48
  • @Bergi actually I put it in purpose of showing how it would be in such synchronous platform (that code was translated from Swift language) – Buğra Ekuklu Oct 15 '15 at 18:50
  • 1
    It definitely is a bad practice to [use `new Promise`](http://stackoverflow.com/q/23803743/1048572) at all. – Bergi Oct 15 '15 at 18:50
  • No dumb questions, only dumb answers :) – ryuu9187 Oct 15 '15 at 19:24

1 Answers1

2

I can't see any async method in your first example. Array.forEach is not a async function. You just return the course inside of the callback of forEach, but you must return it directly in sectionNeedsCourse:

sectionNeedsCourse(unsafeCourse) {
    var course = this.courses.find(function(course){
      course.couldBeRepresentedBy(unsafeCourse) ? course : false;      
    });
    return course ? course : new Course(unsafeCourse, []);
}

As you can see, i also use find instead of forEach, because this is the logic you need here.

As requested here the same example a little bit shorter with an Arrow function:

sectionNeedsCourse(unsafeCourse) {
    var course = this.courses.find(course => {
      course.couldBeRepresentedBy(unsafeCourse) ? course : false;      
    });
    return course ? course : new Course(unsafeCourse, []);
}
PatrickD
  • 1,136
  • 5
  • 7