0

I have This javascript code does not work normally .

var asyncFunction = function () {

    setTimeout(function () {
        return 'accepted';
    }, Math.floor(Math.random() * 5000));
};
var Applicant;
(Applicant = function (applicant_name_var, applicant_age_var) {
    name = applicant_name_var;
    a = applicant_age_var;
    return {
        who_AM_I: function () {
            if (name == null) { return 'No Name'; }
            else {
                return name;
            }
        },
        isAdult: function () {
            if (a == null) {
                return false;
            } else
                if (a < 18) {
                    return false;
                } else {
                    return true;
                }
        },
        INTERVIEWRESULT: function () {
            var result = 'pending';
            result = asyncFunction();
            return result;
        }
    };
}).call();
console.log('debut');
var pending_APPLICANT = [];
var accepted_APPLICANT = [];
var applicant1 = new Applicant('John Doe');
var applicant2 = new Applicant();
var applicant3 = new Applicant('Jane Doe', 24);
pending_APPLICANT.push(applicant1);
pending_APPLICANT.push(applicant2);
pending_APPLICANT.push(applicant3);
if (applicant1.INTERVIEWRESULT() == 'accepted') {
    accepted_APPLICANT.push(applicant1);
    pending_APPLICANT.splice(1, 2);
}
if (applicant2.INTERVIEWRESULT() == 'accepted') {
    accepted_APPLICANT.push(applicant2);
    pending_APPLICANT.splice(1, 1);
}
if (applicant3.INTERVIEWRESULT() == 'accepted') {
    accepted_APPLICANT.push(applicant3);
    pending_APPLICANT.splice(0, 1);
}
console.log('Pending applicants:');
for (var i = 0; i < pending_APPLICANT.length; i++) {
    console.log(pending_APPLICANT[i].toString());
}
console.log('Accepted applicants:');
for (var i = 0; i < accepted_APPLICANT.length; i++) {
    console.log(accepted_APPLICANT[i].toString());
}

the output of this code is :

> debut

> Pending applicants:

> [object Object]

> [object Object]

> [object Object]

> Accepted applicants:

The expected output is something like that:

> Pending applicants:

> No one.

> Accepted applicants:

> Name: Jane Doe | Age: 24 | Adult: true

> Name: John Doe | Age: Unknown | Adult: false

> Name: No Name | Age: Unknown | Adult: false

I think the problem is in the asyuncFunction()

Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52

1 Answers1

0

This:

var asyncFunction = function () {

    setTimeout(function () {
        return 'accepted';
    }, Math.floor(Math.random() * 5000));
};
result = asyncFunction();

Is not going to do what you'd expect.

You can not return something that would be determined in a async task.

Please take a look at this question.

What you can do is to pass a callback function to asyncFunction and call it with desired value when it resolved.

var asyncFunction = function (cb) {
    setTimeout(function () {
        cb('accepted');
    }, Math.floor(Math.random() * 5000));
};

It require more changes to your code, but you get the idea.

Here is how you can use the function later on in your code:

asyncFunction(function (result) {
    console.log(result); //accepted
});
Community
  • 1
  • 1
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52