0

I have a method methFirst which is calling another method methSecond, but the problem is that methFirst is getting returned without waiting for methSecond to complete execution -

main method

methMain() {
    var result = methFirst();
    console.log(result); //undefined
}

first method

methFirst(obj) {
    methSecond(obj).then((result) => {
        return result;
    )};
}

second method

methSecond(obj){
    return new Promise((resolve, reject) => {
        if(obj == null) {
            resolve(true);
        }
    )};
}

I understand one way of doing this is returning a promise from methFirst but I do want to use it, any help would be appropriated.

Don
  • 503
  • 1
  • 4
  • 14
  • Your `firstMethod` does not `return` anything? – Bergi Sep 19 '17 at 12:46
  • There is no way around promises. You have to use them. You cannot make it return synchronously (that's not what promises do). – Bergi Sep 19 '17 at 12:47
  • Yes please let me know how can I handle that – Don Sep 19 '17 at 12:48
  • Write `methMain() { firstMethod().then(function(result) { console.log(result); }); }` and `function firstMethod() { return secondMethod(); }` – Bergi Sep 19 '17 at 12:49
  • is it possible any how by not using promise at all and removing it from `methSecond()` also ? – Don Sep 19 '17 at 13:15
  • Well obviously in your example you could just write `function methSecond(obj) { if(obj == null) return true; }` in your example, but I understood you wanted to do something asynchronous - and no, promises are your best bet then. – Bergi Sep 19 '17 at 13:20

0 Answers0