JS承诺:履行与解决 [英] JS Promises: Fulfill vs Resolve

查看:192
本文介绍了JS承诺:履行与解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白承诺存在于三个州之一:承诺可以是待定(未解决),已满足(已成功解决)或已拒绝 (解决不成功)。



阅读 A + Promise Spec MDN的文档,我很困惑,他们都承认履行拒绝状态,但在Promise构造函数的定义中,他们指定了两个回调: resolve 拒绝。看来我们可以互换使用这两个术语;他们不是。



不意味着成功:

  re解决/rəzälv/动词
1.解决或找到解决方案(问题,争议或争议事项)。






意味着成功:

  ful·fill /fo͝olfil/ verb 
1.完成或现实;实现或实现(希望,承诺或预测的东西)。
2.根据需要,承诺或预期执行(任务,职责或角色)。

为什么我们在这里使用解决方案履行承诺?有没有一个实例,我们传递给解决的值可能会导致Promise被拒绝 ed?

解决方案

解析承诺的值为错误不会自动转换承诺拒绝承诺



  var p = Promise.resolve(new Error(rejected)); p.then(function(data){console.log(data,p)}) 






另请参见州和命运







如果您可以包含有关如何解决可能用于
的信息拒绝承诺而不是履行它,我会认为这是
完整答案。


不确定使用解决方案的原因或预期结果拒绝承诺?尽管最简单的方法是将拒绝作为参数,当解析调用 Promise .resolve(Promise.reject(/ * Error here * /))



  var _reject = function(err) {return Promise.reject(err)} var resolver = function(resolve,reject){return resolve(_reject(new Error(reject in resolve)))} var p = new Promise(resolver); p.then (数据){console.log(resolved,data)},function(data){console.log(rejected:,data,promise:,p)}) pre> 



如果预期的结果是捕获错误或被拒绝的 Promise 传递给 resolve 可以附加处理程序,同时保持已解决 PromiseStatus 可以使用 unhandledrejection 事件, Promise.reject 直接或铬/铬49+可以使用 PromiseRejectionEvent



  window.addEventListener unhandledrejection,function(event){//处理未处理的拒绝`Promise` console.log(unhandledrejection:,event.reason,event.promise);}); Promise.resolve(new PromiseRejectionEvent(Error //未处理的`reject`` Promise` promise:Promise.reject(new Error(custom rejection)),reason:custom rejection})).then(function(data){//`PromiseRejectionEvent`包含一个`拒绝`//'Promise`,它触发`unhandledrejection`事件//处理被拒绝的`Promise`这里,解析``PromiseRejectionEvent` console.log(resolved:,data)的`.promise` //对象; },function(err){console.log(rejected,err)}) 



也可以 throw 承诺构造函数,而不使用解析拒绝,应由 onRejected处理 catch



  new Promise(function(resolve,reject)抛出新的错误(Promise constructor中的拒绝)})// catch here这里处理`Promise`构造函数中的````````````````````````````` `.then()`// .catch(function(e){// console.log(catch error:,e); / *返回e:将`e`传递给`.then() ``Promise` * / / * throw e:将````````````````````````````` ,data)},function(err){console.log(rejected:,err ); throw(function(e){console.log(catch error:,e); / *返回e:将`e`传递给`.then()`as`resolved``Promise` * / * throw e:将`e`传递给`.then()`为`rejected`` Promise` * /}) 

/ p>




说明:处理解决和拒绝的一些方法有很多方法 Promise 对象,具体取决于应用程序和预期结果。


I understand Promises to exist in one of three states: A Promise can either be pending (unresolved), fulfilled (resolved successfully) or rejected (resolved unsuccessfully).

Reading through the A+ Promise Spec and MDN's documentation, I am confused that they both acknowledge the fulfilled and rejected states but in the definition of the Promise constructor they specify two callbacks: resolve and reject. It seems we're using these two terms interchangeably; they are not.

Does not imply success:

re·solve /rəˈzälv/ verb
1. settle or find a solution to (a problem, dispute, or contentious matter).


Does imply success:

ful·fill /fo͝olˈfil/ verb
1. bring to completion or reality; achieve or realize (something desired, promised, or predicted).
2. carry out (a task, duty, or role) as required, pledged, or expected.

Why are we using resolve here when we're actually fulfilling the Promise? Is there an instance in which the value we pass to resolve might result in the Promise being rejected?

解决方案

A resolved Promise having a value of an Error does not automatically convert the Promise to rejected Promise

var p = Promise.resolve(new Error("rejected"));
p.then(function(data) {
  console.log(data, p)
})


See also States and fates


If you could include information on how resolve might be used to reject the Promise rather than fulfill it, I would consider this a complete answer.

Not certain about reason for or expected result of using resolve to reject a Promise ? Though simplest approach to achieve this would be to pass reject as a parameter when resolve called Promise.resolve(Promise.reject(/* Error here */))

var _reject = function(err) {
  return Promise.reject(err)
}

var resolver = function(resolve, reject) {  
    return resolve(_reject(new Error("reject within resolve")))
}

var p = new Promise(resolver);

p.then(function(data) {
  console.log("resolved", data)
},function(data) {
  console.log("rejected:", data, "promise:", p)
})

If expected result is to catch errors or a rejected Promise passed to resolve where handlers may be attached, while maintaining the "resolved" PromiseStatus , could use unhandledrejection event , Promise.reject directly or at chrome / chromium 49+ could use PromiseRejectionEvent

window.addEventListener("unhandledrejection", function(event) {
  // handle unhandled rejected `Promise`
  console.log("unhandledrejection:", event.reason, event.promise);
});

Promise.resolve(new PromiseRejectionEvent("Error", {
    // unhandled `rejected` `Promise` 
    promise: Promise.reject(new Error("custom rejection")),
    reason: "custom rejection"
  }))
  .then(function(data) {
    // `PromiseRejectionEvent` contains a `rejected`
    // `Promise` , which triggers `"unhandledrejection"` event
    // to handle rejected `Promise` here, resolve `.promise` 
    // object of `PromiseRejectionEvent`
    console.log("resolved:", data);
  }, function(err) {
    console.log("rejected", err)
})

could also throw an Error within Promise constructor without using resolve or reject , which should be handled by onRejected or catch

new Promise(function(resolve, reject) {
  throw new Error("reject within Promise constructor")
})
// catch here handles `Error` from `Promise` constructor
// will be `resolved` at `.then()` if `Error` not `throw`n to `.then()` 
// .catch(function(e) {
//  console.log("caught error:", e);
  /* return e : pass `e` to `.then()` as `resolved` `Promise` */
  /* throw e : pass `e` to `.then()` as `rejected` `Promise`  */
//})

.then(function(data) {
  console.log("resolved:", data)
}, function(err) {
  console.log("rejected:", err);
  throw err
})
.catch(function(e) {
  console.log("caught error:", e);
  /* return e : pass `e` to `.then()` as `resolved` `Promise` */
  /* throw e : pass `e` to `.then()` as `rejected` `Promise`  */
})


Explanation: There are a number of approaches to handling both resolved and rejected Promise objects , depending on application and expected results.

这篇关于JS承诺:履行与解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆