简体   繁体   English

在 Javascript Promise 中解决和拒绝实际上做了什么

[英]What does resolve and reject actually do in Javascript Promise

At first, I think resolve simply pass the parameter to the function in then , so I tried this起初,我认为resolve只是将参数传递给then的函数,所以我尝试了这个

const promise = new Promise((resolve, reject) => {
  resolve(new Promise(resolve => resolve(2333)))
})
// promise.then(innerPromise => {
//   innerPromise.then(num => console.log(num))
// })
promise.then(num => console.log(num))

The lines commented got an error: innerPromise.then is not a function , so I assume resolve will firstly help you handle the promise inside if you have a promise as the paramter注释的行有一个错误: innerPromise.then is not a function ,所以我认为如果你有一个承诺作为参数, resolve将首先帮助你处理内部的承诺

So I tried reject , I think it will be the same所以我尝试了reject ,我认为它会是一样的

const promise = new Promise((resolve, reject) => {
  reject(new Promise(resolve => resolve(2333)))
})
promise.then(null, innerPromise => {
  innerPromise.then(num => console.log(num))
})
// promise.then(null, num => console.log(num))

The lines uncommented will log 2333 , the lines commented will simply log the rejected Promise instance未注释的行将记录2333 ,注释的行将仅记录被拒绝的Promise实例

Resolve is a callback used to return the value or the result of another promise. Resolve 是一个回调函数,用于返回另一个 promise 的值或结果。

So when you execute the below code in first block:因此,当您在第一个块中执行以下代码时:

const promise = new Promise((resolve, reject) => {
  resolve(new Promise(resolve => resolve(2333)))
})

promise.then(innerPromise => {
      console.log(innerPromise)
//    innerPromise.then(num => console.log(num))
})

promise.then(num => console.log(num))

You will find the inner functions are executed automatically by the Resolve, thus getting the final result of functions inside resolve.你会发现内部函数是由Resolve自动执行的,从而得到resolve内部函数的最终结果。 Secondly, it is clearly mentioned that 'Resolve' returns a 'Value' and not a callable object.其次,明确提到“解决”返回“值”而不是可调用对象。 So, you cannot call the result of a promise like a function.因此,您不能像调用函数那样调用 promise 的结果。

In case of Reject, reject callback is used to reject the promise with a provided reason or error.在拒绝的情况下,拒绝回调用于拒绝具有提供的原因或错误的承诺。 You may return any statement (that represents error) or a callable object that returns you the error message from error code.您可以返回任何语句(表示错误)或从错误代码返回错误消息的可调用对象。 (That's how I use the Reject to fetch the messages from Error codes generated) (这就是我使用 Reject 从生成的错误代码中获取消息的方式)

Well, you can read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise好吧,你可以在这里阅读更多内容: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

By the defination, resolve can take value and thenable value.根据定义,resolve can take value 和 thenable value。 If thenable value, it will resolve and return the state.如果 thenable 值,它将解析并返回状态。 However, reject only take as value.但是,拒绝仅作为值。 Even if value it promise(thenable).即使重视它承诺(thenable)。 It wont resolve it.它不会解决它。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve

Argument to be resolved by this Promise.由这个 Promise 解决的参数。 Can also be a Promise or a thenable to resolve也可以是 Promise 或 thenable to resolve

The Promise.resolve() method returns a Promise object that is resolved with a given value. Promise.resolve() 方法返回一个使用给定值解析的 Promise 对象。 If the value is a promise, that promise is returned;如果值是一个promise,则返回该promise; if the value is a thenable (ie has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state;如果值是 thenable(即有一个“then”方法),则返回的 promise 将“跟随”那个 thenable,采用它的最终状态; otherwise the returned promise will be fulfilled with the value.否则返回的承诺将用该值实现。 This function flattens nested layers of promise-like objects (eg a promise that resolves to a promise that resolves to something) into a single layer.该函数将嵌套的类承诺对象层(例如,解析为解析为某事的承诺的承诺)扁平化为单个层。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject

The Promise.reject() method returns a Promise object that is rejected with a given reason. Promise.reject() 方法返回一个因给定原因被拒绝的 Promise 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM