0

Hi i want to use async await without any callback wrapper like

.then(function(){});

or

something(anyparams,function(){});

in other words i want to use my code line by line by converting promise into async await

Problem: i'm successfully converted promise into async await if block is going wrong, how to fix that. expected output is "result: FFF"

Here is my code:

function getName () {
    return new Promise(function (resolve, rejected) {
        var name = "FFF";
        // wait 3000 milliseconds before calling resolve
        setTimeout ( 
            function() {
                resolve( name )
            }, 3000)
    })
}


async function foo () {
   console.log('Before Name');
   var name = await getName();
   console.log('After Name');
  return name;
}

var str = foo();

if(str == 'FFF'){
    alert('result: '+str);
}else{
   alert('entered wrong block');
}
EaB
  • 43
  • 9

1 Answers1

1

You need to await your async function call as well like so:

var str = await foo();

vzwick
  • 11,008
  • 5
  • 43
  • 63