1

Hello I have this sheet of Javascript code :

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

var Applicant = function (applicant_name_var, applicant_age_var) {
    var name = applicant_name_var;
    var a = applicant_age_var;
    return {
        who_AM_I: function () {
            if (name == null) { return 'No Name'; }
            else {
                return name;
            }
        },
        INTERVIEWRESULT: function () {
            var result = 'pending';
            asyncFunction(function (result) {
             result = result;
            });
            return result;
        }
    };
};

What I am looking for is to change the value of result in INTERVIEWRESULT from "pending" to "accepted".

Please could you help me to fix the code above

akram1rekik
  • 153
  • 1
  • 1
  • 7
  • Wrap result into an object. You'll be able to return that object and change the status within the asyncFunction scope. Do it like: {status: 'pending'}. You should return a promise instead of the value, though. – EProgrammerNotFound Dec 15 '15 at 15:33
  • sorry I didn't understand I still a beginner in Js ^^ – akram1rekik Dec 15 '15 at 15:38
  • You should study promises. such as [q](https://github.com/kriskowal/q) or [ECMA 6's](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) – EProgrammerNotFound Dec 15 '15 at 15:40
  • It's totally impossible. When you `return` from the function, the string `'accepted'` has not yet been created. You cannot peek into the future. – Bergi Dec 15 '15 at 15:41
  • Since it's an asynchronous method you aren't supposed to return a value, instead, return a promise of the value. – EProgrammerNotFound Dec 15 '15 at 15:41

0 Answers0