0
Q:

nodejs promise async

// Normal Function
function add(a,b){
  return a + b;
}
// Async Function
async function add(a,b){
  return a + b;
}
1
// server.js
 
function square(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(Math.pow(x, 2));
    }, 2000);
  });
}
 
async function layer(x)
{
  const value = await square(x);
  console.log(value);
}
 
layer(10);
1
//you can only return a value from an async function by passing in a callback function like so: 
function longRunningFunction(param1, callback){
    setTimeout(function(){
         var results="O High there!";
         callback(results);
    }, 2000);
} 

//then call the async function and pass the callback function like so
longRunningFunction("morning", function(result){
  alert(result);
});
-1

New to Communities?

Join the community