处理JavaScript中被遗忘的承诺 [英] Dealing with forgotten promises in JavaScript

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

问题描述

这里有一个例子,希望可以简化问题的理解.

Here is an example that will hopefully make understaning the question easier.

var listen = document.querySelector("#listen"),
  cancel = document.querySelector("#cancel"),
  submit = document.querySelector("#submit");

var promiseResolve = null;

listen.addEventListener("click", startListening);
cancel.addEventListener("click", abort);
submit.addEventListener("click", onSubmitClick);
submit.disabled = true;

function startListening() {
  submit.disabled = false;
  listen.disabled = true;
  new Promise(function(resolve) {
    promiseResolve = resolve;
  }).then(onSubmit);
}

function abort() {
  listen.disabled = false;
  submit.disabled = true;
  promiseResolve = null;
}

function onSubmitClick() {
  if (promiseResolve) promiseResolve();
}

function onSubmit() {
  console.log("Done");
  abort();
}

<button id="listen">Listen</button>
<button id="submit">Submit</button>
<button id="cancel">Cancel</button>

在上面的脚本中,有一个动作(listen),将在Promise的帮助下启用另一个动作(submit).但是可以使用cancel操作取消​​该流程,将大多数代码恢复为其原始状态. cancel操作仅将Promiseresolve的引用设置为null,这意味着Promise将永远处于混乱状态,因为它将永远不会被解决或拒绝.这些是我的问题:

In the script above there is an action (listen) that will enable another action (submit), using the help of a Promise. But that flow can be cancelled using the cancel action, returning most of the code to its original state. The cancel action only sets the reference of the resolve of the Promise to null, meaning that the promise will forever stay in limbo as it will never be resolved or rejected. So these are my questions:

  • 这种方法正确吗?
  • 这样做会占用大量资源吗?
  • 我还应该保留对拒绝函数的引用,并在cancel动作中调用它吗?
  • Is this approach correct?
  • Would doing this a large amount of times, occupy an equally large amount of resources?
  • Should I also keep a reference to the reject function and call that in the cancel action?

我知道在上面的示例中,仅通过以下操作就可以实现相同的结果 使用boolean标志检查是否已按下监听按钮 在提交之前,但是就像我说的那样,这只是一个例子,所以我 可以更轻松地解释问题.

I know in the example above the same result can be achieved by just using a boolean flag to check if the listen button has been pressed before the submit, but like I said this is just an example so that I can more easily explain the question.

推荐答案

答案中所述;

大多数使用promise的代码都希望它们在某些情况下能够解决或拒绝 指向未来(这就是为什么首先使用诺言的原因). 如果他们不这样做,那么该代码通常将永远无法完成其工作.

Most code that uses promises expects them to resolve or reject at some point in the future (that's why promises are used in the first place). If they don't, then that code generally never gets to finish its work.

您可能还有其他一些代码可以完成 为那个任务而努力,而诺言却从未被放弃 做它的事情.如果您这样做,则Javascript中没有内部问题 这样,但不是设计诺言的方式,而是 通常,诺言的使用者并不期望诺言如何运作.

It's possible that you could have some other code that finishes the work for that task and the promise is just abandoned without ever doing its thing. There's no internal problem in Javascript if you do it that way, but it is not how promises were designed to work and is generally not how the consumer of promises expect them to work.

无法解决或拒绝承诺不会在Javascript中引起问题,但是无论如何都是不好的做法.您的应用程序如果无法解决,就无法确定诺言发生了什么.而不是让promise陷入困境,而是返回一个错误消息之类的值,并为错误消息提供promise过滤器结果.如果发现错误,请拒绝()诺言,以便应用程序确定下一步.

Failing to resolve or reject a promise doesn't cause a problem in Javascript, but it's a bad practice anyway. Your application cannot determine what happened to the promise if it never resolves. Instead of leaving the promise in limbo, return a value like an error message, and have the promise filter results for an error message. If it finds an error, reject() the promise so the application can determine its next move.

var listen = document.querySelector("#listen"),
  cancel = document.querySelector("#cancel"),
  submit = document.querySelector("#submit");

var promiseResolve = null;

listen.addEventListener("click", startListening);
cancel.addEventListener("click", onCancelClick);
submit.addEventListener("click", onSubmitClick);
submit.disabled = true;

function startListening() {
  submit.disabled = false;
  listen.disabled = true;
  new Promise(function(resolve, reject) {
    promiseResolve = (error) => {
       if (error) { reject(error); } else { resolve(); }
    };
  }).then(onSubmit)
  .catch(error => onError(error));
}

function abort() {
  listen.disabled = false;
  submit.disabled = true;
  promiseResolve = null;
}

function onSubmitClick() {
  if (promiseResolve) promiseResolve();
}

function onCancelClick() {
  if (promiseResolve) promiseResolve("Cancelled!");
}

function onSubmit() {
  console.log("Done");
  abort();
}

function onError(error) {
  console.warn(error);
  abort();
}

<button id="listen">Listen</button>
<button id="submit">Submit</button>
<button id="cancel">Cancel</button>

这篇关于处理JavaScript中被遗忘的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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