Promise is synchronous or asynchronous in node js

前端 未结 6 1961
滥情空心
滥情空心 2020-12-10 02:47

I have lot of confusion in promise. It\'s a synchronous or asynchronous ?

return new Promise (function(resolved,reject){
    //sync or async? 
});
         


        
6条回答
  •  伪装坚强ぢ
    2020-12-10 03:00

    When you create a promise and pass a call back to it that callback is gonna executed immediately (sync)

    const promise= new Promise(function(resolve, reject) {
          //doing some logic it gonna be executed synchronously
           console.log("result");
        })
        console.log("global log")
        

    But when you resolve it by .then() method it will act in asynchronous way so for example :

    const promise = new Promise(function(resolve, reject) {
      //doing some logic it gonna be executed synchronously
    
      resolve("fullfiled")
    })
    promise.then(v => {
      console.log(v)
    })
    console.log("global log")

提交回复
热议问题