user defined function with callback in nodejs

前端 未结 2 1491
抹茶落季
抹茶落季 2021-02-14 03:13

can anyone give me an example in which we are creating a particular function, which is also having a callback function ?

function login(username, password, funct         


        
2条回答
  •  醉梦人生
    2021-02-14 03:57

    bad question but w/e
    you have mixed up invoking and defining an asynchronous function:

    // define async function:
    function login(username, password, callback){
      console.log('I will be logged second');
      // Another async call nested inside. A common pattern:
      setTimeout(function(){
        console.log('I will be logged third');
        callback(null, {});
      }, 1000);
    };
    
    //  invoke async function:
    console.log('I will be logged first');
    login(username, password, function(err,result){
      console.log('I will be logged fourth');
      console.log('The user is', result)
    });
    

提交回复
热议问题