从函数返回值和返回promip .resolve()之间的区别

时间:2021-01-20 20:11:48

I have problem in understanding that what happens when we simply return a value or when we return Promise.resolve() from a function. Specifically: I am trying to understand how promises chaining works. I am chaining methods and verifying whether the value reaches in the method which is last call to then. I just want to understand the difference between returning promises to then, returning Promise.resolve() to then, and returning just a value to then.

我有一个问题,那就是当我们返回一个值或者返回一个函数时,会发生什么。具体来说:我正在尝试理解承诺链接是如何工作的。我正在对方法进行链接,并验证该值是否在方法中到达,这是最后一次调用。我只是想理解返回到then的承诺、返回到then的承诺.resolve()和返回到then的值之间的区别。

3 个解决方案

#1


3  

I have problem in understanding that what happens when we simply return a value or when we return Promise.resolve() from a function.

我在理解当我们简单地返回一个值或者从一个函数返回promisee .resolve()时会发生什么方面有问题。

So there are (at least) two different scenarios:

所以至少有两种不同的情况:

In just any old function

If it's just any old function (not a then or catch callback), then the difference is that return value; directly returns the value, whereas return Promise.resolve(value); returns a promise resolved with that value. It changes how the calling code uses the result. In the return value; case, the calling code just directly uses it. In the return Promise.resolve(value); case, the calling code needs to consume the promise, just like any other promise (and can't assume that the promise is already settled, even though it is).

如果它只是一个旧函数(不是then或catch回调),那么不同之处在于返回值;直接返回值,而返回promisee .resolve(value);返回一个用该值解析的承诺。它改变了调用代码使用结果的方式。返回值;case,调用代码直接使用它。在返回Promise.resolve(价值);案例中,调用代码需要使用承诺,就像其他承诺一样(并且不能假设承诺已经被解决,即使它已经被解决)。

If the function is meant to be the start of a promise chain, you'd use return Promise.resolve(value);. (There are use cases for this; for instance, if the function may or may not need to start an asynchronous process: In the branch that doesn't have to, you'd still return a promise because the branch that has to do something asynchronous has to return a promise.) If it isn't, you'd just use return value;.

如果函数是一个承诺链的开始,您应该使用return Promise.resolve(value);。(有这样的用例;例如,如果函数可能需要也可能不需要启动异步进程:在不需要启动异步进程的分支中,您仍然会返回一个承诺,因为必须执行异步操作的分支必须返回一个承诺。如果不是,就用return value;。

In then or catch callback

I am trying to understand how promises chaining works.

我试图理解承诺链接是如何工作的。

In that case, you're talking about return value; vs. return Promise.resolve(value); in a then or catch callback. That's easy: There's no point at all to return Promise.resolve(value); in a then or catch callback, it's redundant and unnecessary overhead; just use return value;.

在这种情况下,你说的是返回值;与返回Promise.resolve(价值);然后或捕获回调。这很简单:没有必要返回承诺。在then或catch回调中,它是冗余和不必要的开销;只是使用返回值;。

then and catch always return promises. If your callback returns a simple value (return value;), the promise is resolved with that value. If your callback returns a thenable (loosely, a promise; e.g., return Promise.resolve(value);), then the promise returned by then/catch is slaved to that thenable: When the thenable is settled, the promise is settled the same way. (And if your then or catch callback throws an error, the promise is rejected with that error.)

然后再接再接。如果回调返回一个简单的值(返回值;),则承诺用该值进行解析。如果回调返回一个thenable(松散地,一个承诺;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,(如果您的回调或捕获回调抛出一个错误,则该错误将被拒绝。)

There are valid reasons to return a promise from a then or catch callback, if you're starting a new asynchronous process in that callback; but if you have an immediate value ready, there's no point in wrapping it in a promise — then and catch already do that.

如果您正在那个回调中启动一个新的异步进程,那么有充分的理由从then或catch回调返回一个承诺;但是,如果你已经准备好了一个即时的价值,那么就没有必要把它包装成一个承诺——然后抓住它。

#2


3  

When you return Promise.resolve(), you wrap your result into a Promise, on which you can call then and make chain of then. This is preferable for a single type returns.

当您返回promisee .resolve()时,您将结果包装成一个承诺,您可以在该承诺上调用该承诺,然后进行连锁操作。对于单个类型的返回,这是更可取的。

function resolve() {
   return Promise.resolve(15);
}

resolve().then(value => console.log(value));

If you have a logic which depends on a call to the server and also you can have that result in the cache, you can have a function which in all cases returns you a Promise, so you can add then for the result of the Promise. Look at this pseudo-code.

如果你有一个逻辑,它依赖于对服务器的调用,并且你也可以在缓存中得到这个结果,你可以有一个函数,在所有情况下,它会返回你的一个承诺,所以你可以添加,然后为这个承诺的结果。看看这个伪代码。

I want my function in all cases return a Promise. So here if I have cached the userId I can just wrap the result into the Promise with Promise.resolve

我希望我的函数在所有情况下都能返回一个承诺。在这里,如果我缓存了userId,我可以用Promise.resolve将结果打包成承诺

function getUserData() {
   if(cache.hasUserId) {
      return Promise.resolve(cache.hasUserId); // Is used to let the `then` functions to be called
   } else {
      return myAjaxCall();
   }
}

getUserData().then(userId => /* */);

Base on comment. Wrap into the Promise.resolve()

基础上发表评论。包装成Promise.resolve()

function add1(data) { 
  return new Promise(function (resolve, reject) { 
               resolve(data + 1); 
         }); 
} 

function add2(data) { 
   return Promise.resolve(data + 2); 
} 

function printdata(data) { 
   console.log(data); 
}

#3


0  

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future. MDN

承诺是一种价值的代理,它不需要在创建承诺时就知道。它允许您将处理程序与异步操作的最终成功值或失败原因关联起来。这允许异步方法返回像同步方法这样的值:异步方法返回的是在将来某个时刻提供值的承诺,而不是立即返回最终值。中数

The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; if the value was a promise, that object becomes the result of the call to Promise.resolve; otherwise the returned promise will be fulfilled with the value. MDN Promise.reslove

resolve(value)方法返回一个Promise对象,该对象与给定的值一起解析。如果该值是thenable(即具有“then”方法),则返回的承诺将“follow”该值,采用其最终状态;如果该值是一个承诺,那么该对象将成为对Promise.resolve的调用的结果;否则,返回的承诺将被价值实现。MDN Promise.reslove

Returning a value from a function is synchronous unlike Promise.resolve()

从函数返回值是同步的,与promisee .resolve()不同

#1


3  

I have problem in understanding that what happens when we simply return a value or when we return Promise.resolve() from a function.

我在理解当我们简单地返回一个值或者从一个函数返回promisee .resolve()时会发生什么方面有问题。

So there are (at least) two different scenarios:

所以至少有两种不同的情况:

In just any old function

If it's just any old function (not a then or catch callback), then the difference is that return value; directly returns the value, whereas return Promise.resolve(value); returns a promise resolved with that value. It changes how the calling code uses the result. In the return value; case, the calling code just directly uses it. In the return Promise.resolve(value); case, the calling code needs to consume the promise, just like any other promise (and can't assume that the promise is already settled, even though it is).

如果它只是一个旧函数(不是then或catch回调),那么不同之处在于返回值;直接返回值,而返回promisee .resolve(value);返回一个用该值解析的承诺。它改变了调用代码使用结果的方式。返回值;case,调用代码直接使用它。在返回Promise.resolve(价值);案例中,调用代码需要使用承诺,就像其他承诺一样(并且不能假设承诺已经被解决,即使它已经被解决)。

If the function is meant to be the start of a promise chain, you'd use return Promise.resolve(value);. (There are use cases for this; for instance, if the function may or may not need to start an asynchronous process: In the branch that doesn't have to, you'd still return a promise because the branch that has to do something asynchronous has to return a promise.) If it isn't, you'd just use return value;.

如果函数是一个承诺链的开始,您应该使用return Promise.resolve(value);。(有这样的用例;例如,如果函数可能需要也可能不需要启动异步进程:在不需要启动异步进程的分支中,您仍然会返回一个承诺,因为必须执行异步操作的分支必须返回一个承诺。如果不是,就用return value;。

In then or catch callback

I am trying to understand how promises chaining works.

我试图理解承诺链接是如何工作的。

In that case, you're talking about return value; vs. return Promise.resolve(value); in a then or catch callback. That's easy: There's no point at all to return Promise.resolve(value); in a then or catch callback, it's redundant and unnecessary overhead; just use return value;.

在这种情况下,你说的是返回值;与返回Promise.resolve(价值);然后或捕获回调。这很简单:没有必要返回承诺。在then或catch回调中,它是冗余和不必要的开销;只是使用返回值;。

then and catch always return promises. If your callback returns a simple value (return value;), the promise is resolved with that value. If your callback returns a thenable (loosely, a promise; e.g., return Promise.resolve(value);), then the promise returned by then/catch is slaved to that thenable: When the thenable is settled, the promise is settled the same way. (And if your then or catch callback throws an error, the promise is rejected with that error.)

然后再接再接。如果回调返回一个简单的值(返回值;),则承诺用该值进行解析。如果回调返回一个thenable(松散地,一个承诺;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,(如果您的回调或捕获回调抛出一个错误,则该错误将被拒绝。)

There are valid reasons to return a promise from a then or catch callback, if you're starting a new asynchronous process in that callback; but if you have an immediate value ready, there's no point in wrapping it in a promise — then and catch already do that.

如果您正在那个回调中启动一个新的异步进程,那么有充分的理由从then或catch回调返回一个承诺;但是,如果你已经准备好了一个即时的价值,那么就没有必要把它包装成一个承诺——然后抓住它。

#2


3  

When you return Promise.resolve(), you wrap your result into a Promise, on which you can call then and make chain of then. This is preferable for a single type returns.

当您返回promisee .resolve()时,您将结果包装成一个承诺,您可以在该承诺上调用该承诺,然后进行连锁操作。对于单个类型的返回,这是更可取的。

function resolve() {
   return Promise.resolve(15);
}

resolve().then(value => console.log(value));

If you have a logic which depends on a call to the server and also you can have that result in the cache, you can have a function which in all cases returns you a Promise, so you can add then for the result of the Promise. Look at this pseudo-code.

如果你有一个逻辑,它依赖于对服务器的调用,并且你也可以在缓存中得到这个结果,你可以有一个函数,在所有情况下,它会返回你的一个承诺,所以你可以添加,然后为这个承诺的结果。看看这个伪代码。

I want my function in all cases return a Promise. So here if I have cached the userId I can just wrap the result into the Promise with Promise.resolve

我希望我的函数在所有情况下都能返回一个承诺。在这里,如果我缓存了userId,我可以用Promise.resolve将结果打包成承诺

function getUserData() {
   if(cache.hasUserId) {
      return Promise.resolve(cache.hasUserId); // Is used to let the `then` functions to be called
   } else {
      return myAjaxCall();
   }
}

getUserData().then(userId => /* */);

Base on comment. Wrap into the Promise.resolve()

基础上发表评论。包装成Promise.resolve()

function add1(data) { 
  return new Promise(function (resolve, reject) { 
               resolve(data + 1); 
         }); 
} 

function add2(data) { 
   return Promise.resolve(data + 2); 
} 

function printdata(data) { 
   console.log(data); 
}

#3


0  

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future. MDN

承诺是一种价值的代理,它不需要在创建承诺时就知道。它允许您将处理程序与异步操作的最终成功值或失败原因关联起来。这允许异步方法返回像同步方法这样的值:异步方法返回的是在将来某个时刻提供值的承诺,而不是立即返回最终值。中数

The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; if the value was a promise, that object becomes the result of the call to Promise.resolve; otherwise the returned promise will be fulfilled with the value. MDN Promise.reslove

resolve(value)方法返回一个Promise对象,该对象与给定的值一起解析。如果该值是thenable(即具有“then”方法),则返回的承诺将“follow”该值,采用其最终状态;如果该值是一个承诺,那么该对象将成为对Promise.resolve的调用的结果;否则,返回的承诺将被价值实现。MDN Promise.reslove

Returning a value from a function is synchronous unlike Promise.resolve()

从函数返回值是同步的,与promisee .resolve()不同