Hasan Setiawan

Write, write, write give your wings on code!

Follow me on GitHub

Javascript escaping value of async await function

Async await it's a generator that return Promise. So in case you want to escaping the value of your async await oparation, You can use side effect. By overriding the global state with Promise result from async function.

            
var result;
function add(a,b) {
    return a += b;
}

function multiple(c,d) {
    return c *= d;
}

async function operation(e,f) {
    var g = await add(e,f);
    var h = await multiple(e,f);
    return Promise.resolve(g + h);
}

var operate = operation(10, 20);
operate.then((value) => {
  //global state overriding
  result = value;
});

// Promise values are escaped
console.log(result);

//will give you log:
//230