Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么是未处理的拒绝承诺?_Javascript_Angular_Es6 Promise_Spawn_Unhandled Exception - Fatal编程技术网

Javascript 什么是未处理的拒绝承诺?

Javascript 什么是未处理的拒绝承诺?,javascript,angular,es6-promise,spawn,unhandled-exception,Javascript,Angular,Es6 Promise,Spawn,Unhandled Exception,为了学习Angular 2,我正在尝试他们的教程 我遇到了这样的错误: (node:4796) UnhandledPromiseRejectionWarning: Unhandled promise rejection (r ejection id: 1): Error: spawn cmd ENO

为了学习Angular 2,我正在尝试他们的教程

我遇到了这样的错误:

(node:4796) UnhandledPromiseRejectionWarning: Unhandled promise rejection (r                                                                                                     ejection id: 1): Error: spawn cmd ENOENT
[1] (node:4796) DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate the Node.
js process with a non-zero exit code.
我在SO中查看了不同的问题和答案,但没有发现什么是“未经处理的拒绝承诺”


有谁能简单地解释一下它是什么,以及什么是
错误:spawn cmd enoint
是什么,当它出现时,我必须检查什么来消除这个警告?

这是当
承诺
.reject()
完成时,或者在
异步
执行的代码中抛出异常,没有
.catch()
没有处理拒绝

被拒绝的承诺就像是一个异常,它向应用程序入口点冒泡,并导致根错误处理程序生成该输出

另见


这是在
承诺
通过
.reject()
完成时,或者在
异步
执行的代码中引发异常,并且没有
.catch()
处理拒绝

被拒绝的承诺就像是一个异常,它向应用程序入口点冒泡,并导致根错误处理程序生成该输出

另见


此错误的根源在于每个承诺都需要处理承诺拒绝,即有一个.catch(…)。您可以通过向代码中的承诺添加.catch(…)来避免相同的情况,如下所示

例如,函数PTest()将根据全局变量somevar的值解析或拒绝承诺

在某些情况下,即使我们为承诺编写了.catch(..),也会出现“未处理的承诺拒绝”消息。这都是关于如何编写代码的。以下代码将生成“未处理的承诺拒绝”,即使我们正在处理
catch

var somevar = false;
var PTest = function () {
    return new Promise(function (resolve, reject) {
        if (somevar === true)
            resolve();
        else
            reject();
    });
}
var myfunc = PTest();
myfunc.then(function () {
     console.log("Promise Resolved");
});
// See the Difference here
myfunc.catch(function () {
     console.log("Promise Rejected");
});

不同之处在于,您没有将
.catch(…)
作为链处理,而是单独处理。出于某种原因,JavaScript引擎将其视为承诺而不进行未处理的承诺拒绝。

此错误的根源在于每个承诺都应处理承诺拒绝,即有一个.catch(…)。您可以通过向代码中的承诺添加.catch(…)来避免相同的情况,如下所示

例如,函数PTest()将根据全局变量somevar的值解析或拒绝承诺

在某些情况下,即使我们为承诺编写了.catch(..),也会出现“未处理的承诺拒绝”消息。这都是关于如何编写代码的。以下代码将生成“未处理的承诺拒绝”,即使我们正在处理
catch

var somevar = false;
var PTest = function () {
    return new Promise(function (resolve, reject) {
        if (somevar === true)
            resolve();
        else
            reject();
    });
}
var myfunc = PTest();
myfunc.then(function () {
     console.log("Promise Resolved");
});
// See the Difference here
myfunc.catch(function () {
     console.log("Promise Rejected");
});
不同之处在于,您没有将
.catch(…)
作为链处理,而是单独处理。出于某种原因,JavaScript引擎将其视为承诺,而不进行未处理的承诺拒绝。

承诺被拒绝后可以“处理”。也就是说,可以在提供catch处理程序之前调用promise的拒绝回调。这种行为对我来说有点麻烦,因为一个人可以写

var promise = new Promise(function(resolve) {
kjjdjf(); // this function does not exist });
。。。在这种情况下,承诺被默默地拒绝了。如果忘记添加catch处理程序,代码将继续以静默方式运行,不会出现错误。这可能会导致长期存在且难以找到的bug

在Node.js的例子中,讨论了如何处理这些未处理的承诺拒绝并报告问题。这就引出了ES7异步/等待。考虑这个例子:

async function getReadyForBed() {
  let teethPromise = brushTeeth();
  let tempPromise = getRoomTemperature();

  // Change clothes based on room temperature
  let temp = await tempPromise;
  // Assume `changeClothes` also returns a Promise
  if(temp > 20) {
    await changeClothes("warm");
  } else {
    await changeClothes("cold");
  }

  await teethPromise;
}
在上面的例子中,假设在达到getRoomTemperature之前拒绝了teethPromise(错误:牙膏用完了!)。在这种情况下,将有一个未处理的承诺拒绝,直到等待承诺

我的观点是。。。如果我们认为未处理的拒绝拒绝是一个问题,稍后等待处理的承诺可能会被错误地报告为bug。然后,如果我们考虑未处理的拒绝拒绝不存在问题,可能不会报告合法的bug。 对此有何想法

这与Node.js项目中的讨论有关:

如果以这种方式编写代码:

function getReadyForBed() {
  let teethPromise = brushTeeth();
  let tempPromise = getRoomTemperature();

  // Change clothes based on room temperature
  return Promise.resolve(tempPromise)
    .then(temp => {
      // Assume `changeClothes` also returns a Promise
      if (temp > 20) {
        return Promise.resolve(changeClothes("warm"));
      } else {
        return Promise.resolve(changeClothes("cold"));
      }
    })
    .then(teethPromise)
    .then(Promise.resolve()); // since the async function returns nothing, ensure it's a resolved promise for `undefined`, unless it's previously rejected
}
async function getReadyForBed() {
  let teethPromise = brushTeeth();
  let tempPromise = getRoomTemperature();

  // Change clothes based on room temperature
  var clothesPromise = tempPromise.then(function(temp) {
    // Assume `changeClothes` also returns a Promise
    if(temp > 20) {
      return changeClothes("warm");
    } else {
      return changeClothes("cold");
    }
  });
  /* Note that clothesPromise resolves to the result of `changeClothes`
     due to Promise "chaining" magic. */

  // Combine promises and await them both
  await Promise.all(teethPromise, clothesPromise);
}
当调用getReadyForBed时,它将同步创建最终(未返回)承诺-该承诺将与任何其他承诺具有相同的“未处理拒绝”错误(当然,可能是零,取决于引擎)。(我觉得很奇怪,你的函数没有返回任何东西,这意味着你的异步函数产生了一个未定义的承诺

如果我现在做出承诺而没有捕获,然后添加一个捕获,那么大多数“未处理的拒绝错误”实现在我稍后处理它时实际上会收回警告。换句话说,async/await不会以我所看到的任何方式改变“未处理的拒绝”讨论

为避免此陷阱,请按以下方式编写代码:

function getReadyForBed() {
  let teethPromise = brushTeeth();
  let tempPromise = getRoomTemperature();

  // Change clothes based on room temperature
  return Promise.resolve(tempPromise)
    .then(temp => {
      // Assume `changeClothes` also returns a Promise
      if (temp > 20) {
        return Promise.resolve(changeClothes("warm"));
      } else {
        return Promise.resolve(changeClothes("cold"));
      }
    })
    .then(teethPromise)
    .then(Promise.resolve()); // since the async function returns nothing, ensure it's a resolved promise for `undefined`, unless it's previously rejected
}
async function getReadyForBed() {
  let teethPromise = brushTeeth();
  let tempPromise = getRoomTemperature();

  // Change clothes based on room temperature
  var clothesPromise = tempPromise.then(function(temp) {
    // Assume `changeClothes` also returns a Promise
    if(temp > 20) {
      return changeClothes("warm");
    } else {
      return changeClothes("cold");
    }
  });
  /* Note that clothesPromise resolves to the result of `changeClothes`
     due to Promise "chaining" magic. */

  // Combine promises and await them both
  await Promise.all(teethPromise, clothesPromise);
}
请注意,这应该可以防止任何未处理的承诺拒绝。

承诺被拒绝后可以“处理”。也就是说,可以在提供捕获处理程序之前调用承诺的拒绝回调。这种行为对我来说有点麻烦,因为可以编写

var promise = new Promise(function(resolve) {
kjjdjf(); // this function does not exist });
…在这种情况下,承诺会被默默地拒绝。如果忘记添加捕获处理程序,代码将继续默默地运行,不会出现错误。这可能会导致bug挥之不去,难以发现

在NoDE.js的情况下,有关于处理这些未处理的承诺拒绝并报告问题的讨论。这给我带来了ES7异步/等待。考虑这个例子:

async function getReadyForBed() {
  let teethPromise = brushTeeth();
  let tempPromise = getRoomTemperature();

  // Change clothes based on room temperature
  let temp = await tempPromise;
  // Assume `changeClothes` also returns a Promise
  if(temp > 20) {
    await changeClothes("warm");
  } else {
    await changeClothes("cold");
  }

  await teethPromise;
}
在上面的例子中,假设在达到getRoomTemperature之前拒绝了teethPromise(错误:牙膏用完!)。在这种情况下,在等待teethPromise之前会有一个未处理的Promise拒绝

我的观点是……如果我们认为未经处理的承诺拒绝是一个问题。