改变承诺方法的含义是个好主意吗? [英] Is changing the meaning of promise methods a good idea?

查看:58
本文介绍了改变承诺方法的含义是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Node.js 中有一个长时间运行的方法,它会一直监听命令行输出,直到用户关闭命令或错误停止.长话短说,这个方法有三个回调:

I have a long running method in Node.js that keeps listening for command line output until the user closes the command or an error halts. Long story short, this method had, among others, three callbacks:

  • on_receive:以字符串或转换后的 json 形式将命令行中的任何新输出返回给回调.
  • on_end:当用户终止执行时,我发送这个.
  • on_error:当命令行提示错误时,我将其发送给用户.它总是意味着命令行执行的结束,所以 on_end 在之后被调用(尽管如果需要,它可以在之前被调用).
  • on_receive: Returns any new output from the command line to the callback, either as string or a converted json.
  • on_end: When the execution is terminated by the user, I send this.
  • on_error: When the command line prompts an error, I send it to the user. It always means the end of the command line execution, so on_end is called afterwards (though it could be called before if needed).

在任何时间调用命令后,我都会引用生成的子项,以便稍后杀死它或限制数量.

After any time the command is called, I keep the spawned child referenced, so I can kill it later or limit the amount.

现在,我正计划转向 promises(我正在使用 Kris Kowal 的库,q,但承诺的三条腿并不完全是我的回调(取自他的维基):

Now, I was planning to move to promises (I'm using the library from Kris Kowal, q, but the three legs of the promise are not exactly how my callbacks are (taken from his wiki):

  • resolve:使用已履行的承诺调用 resolve 会导致承诺以传递的承诺的履行价值得到履行.
  • reject:调用有原因的拒绝会导致承诺因该原因被拒绝.
  • notify:使用一个值调用notify 会导致promise 收到该值的进度通知.也就是说,任何使用 promise 注册的 onProgress 处理程序或从 promise 派生的 promise 都将使用进度值调用.
  • resolve: Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value.
  • reject: Calling reject with a reason causes promise to be rejected with that reason.
  • notify: Calling notify with a value causes promise to be notified of progress with that value. That is, any onProgress handlers registered with promise or promises derived from promise will be called with the progress value.

on_error 绑定到 reject 并且可能将 on_end 绑定到 resolve 是有意义的(尽管这也可能来自错误),但在这种情况下要跟踪的最重要的命令 on_receive 实际上与 wiki 所说的进度"无关.

It would make sense to tie on_error to reject and maybe on_end to resolve (although this may also come from an error), but the most important command to track in this case, on_receive, is not actually related to a "progress", as the wiki states.

我做的这段代码是一个供公众使用的node包,所以我不知道这样使用notify是否真的会让其他开发者感到困惑.

This code I'm doing is a node package for public use, so I don't know if using notify in this way would be actually confusing to other developers.

推荐答案

改变 promise 方法的含义是个好主意吗?

Is changing the meaning of promise methods a good idea?

绝对不会.

promise 的三条腿和我的回调不完全一样

the three legs of the promise are not exactly how my callbacks are

他们不需要.大多数承诺甚至没有进步的概念,这就是它在 V2 中被弃用的原因.您唯一需要记住的事情是:promise 代表一个结果,它将异步变为可用.

They don't need to be. Most promises don't even have a notion of progress, that's why it was deprecated in V2. The only thing you need to keep in mind: A promise represent a result that will asynchronously become available.

on_error 绑定到 reject 并且可能将 on_end 绑定到 resolve 是有意义的(尽管这也可能来自一个错误)

It would make sense to tie on_error to reject and maybe on_end to resolve (although this may also come from an error)

不,on_end 不应从错误中调用.承诺只能表示流程成功(已完成)或失败(拒绝).一个进程不能两者兼而有之.

No, on_end should not be called from an error. A promise can only either signal the success of a process (fulfilled) or failure (rejected). A process can't do both.

在这种情况下要跟踪的最重要的命令 on_receive 实际上与进度"无关

the most important command to track in this case, on_receive, is not actually related to a "progress"

是的.进度是一个输出信号.你描述你的 on_receive 回调的方式,它是描述如何处理一些数据的函数的输入.它可能不会被懒惰地提供,但对于函数操作来说必不可少.在这种情况下,它应该只是(停留)一个参数.

Yes. A progress is an output signal. The way you describe your on_receive callback, it is an input to your function that describes how to process some data. It may not be supplied lazily, but is essential to the functions operation. In that case, it should simply be (stay) a parameter.

这篇关于改变承诺方法的含义是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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