Promise is synchronous or asynchronous in node js

前端 未结 6 1962
滥情空心
滥情空心 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:20

    This code makes it clearer:

            console.log("0");
            new Promise((resolve, reject) => {
              console.log("1");
              resolve();
            }).then(() => {
              console.log("2");
            });
            console.log("3");
    

    The code prints: 0 1 3 2 So, then runs asynchronously while the main call back function runs synchronously.

提交回复
热议问题