Promise is synchronous or asynchronous in node js

2019-06-20 01:56发布

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

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

4条回答
冷血范
2楼-- · 2019-06-20 02:09

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")

查看更多
太酷不给撩
3楼-- · 2019-06-20 02:19

Promises are like normal classes in Javascript. Assume you are creating your own Promise implementation, your promise class would roughly look like this. Notice in your constructor you are expecting a method to be passed that you call immediately passing resolve and reject as parameters.

class Promise {
    constructor(method) {
        method(resolve, reject)
    }

    resolve() { ... }

    reject() { ... }

    then() { ... }
}

So when you do new Promise(), you are just creating a new object. Your Promise constructor will run, and it will call the method immediately. So that is why the code inside your promise gets executed synchronously.

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

If inside your function you were calling another function that was async in nature, then that another function would get executed asynchronously, otherwise, everything else gets executed synchronously.

If you had chains in promise using then, then it only gets called after your first promise has called resolve().

return new Promise (function(resolve,reject){
  const a = 5*5; // sync operation.
  db.save(a, function callback() { // async operation.
    resolve() // tells promise to execute `then` block.
  });
});
查看更多
相关推荐>>
4楼-- · 2019-06-20 02:21

Promises aren't exactly synchronous or asynchronous in and of themselves. When you create a promise the callback you pass to it is immediately executed and no other code can run until that function yields. Consider the following example:

new Promise(function(resolve, reject) {
  console.log('foo');
})
console.log('bar');

The code outside the promise has to wait for the code inside the promise (which is synchronous) to complete before it can begin execution.

That said, promises are a common way of dealing with asynchronous code. The most common use case for a promise is to represent some value that's being generated or fetched in an asynchronous fashion. Logic that depends on that value can asynchronously wait until the value is available by registering a callback with .then() or related Promise methods.

查看更多
别忘想泡老子
5楼-- · 2019-06-20 02:30

The function you pass into the Promise constructor runs synchronously, but anything that depends on its resolution will be called asynchronously. Even if the promise resolves immediately, any handlers will execute asynchronously (similar to when you setTimeout(fn, 0)) - the main thread runs to the end first.

This is true no matter your Javascript environment - no matter whether you're in Node or a browser.

console.log('start');
const myProm = new Promise(function(resolve, reject) {
  console.log('running');
  resolve();
});
myProm.then(() => console.log('resolved'));
console.log('end of main block');

查看更多
登录 后发表回答