JS Promises: Fulfill vs Resolve

前端 未结 4 923
我寻月下人不归
我寻月下人不归 2020-12-31 08:07

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

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 09:03

    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.

提交回复
热议问题