将同步功能包装成一个承诺的最佳方法是什么 [英] What is the best way to wrap synchronous functions in to a promise

查看:50
本文介绍了将同步功能包装成一个承诺的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个同步功能,例如 path.join().我想将其包装到 Promise 中,因为我希望在 catch()块中处理异常.如果我像下面那样包装它,则在 Promise .catch()块中不会出现异常.因此,我必须使用 if 来检查返回值是否为错误,然后调用 resolve reject 函数.还有其他解决方案吗?

Let's say I have a synchronous function like path.join(). I want to wrap it into a Promise because I want exceptions to be handled within catch() block. If I wrap it like below, I do not get an exception in the Promise's .catch() block. So I have to use if to check the return value for whether it's an error or not and then call resolve or reject functions. Are there any other solutions?

var joinPaths = function(path1,path2) {
  return new promise(function (resolve, reject) {
    resolve(path.join(path1, path2));
  });
};

推荐答案

目前尚不清楚为什么要将同步操作包装在promise中,因为这只会增加使用难度,并且同步操作已经可以在promise链中使用很好.

It is unclear why you would wrap a synchronous operation in a promise as that just makes it more difficult to use and synchronous operations can already be used within promise chains just fine.

我看到的在同步中做出承诺的唯一两个地方是启动一个promise链,在该链中,后续操作将是异步的,或者当您分支时,分支的一个结果是异步promise,而另一个结果是异步promise是同步的.然后,在这种情况下,您只想在两种情况下都返回一个Promise,以便无论采用哪个分支,调用方都具有一致的异步接口.或者,通用API的不同实现也可能发生这种情况,一个实现是同步的,另一个实现是异步的.该API将被设计为具有异步接口,并且同步实现可能会将自己包装在一个承诺中,以履行通用API的异步合同.

The only two places I've seen it useful to make a promise out of something synchronous are to start a promise chain where subsequent operations will be async or when you are branching and one result of the branch is async promise and the other is synchronous. Then, in that case, you want to just return a promise in both cases so the caller has a consistent async interface no matter which branch is taken. Or, this could also occur with different implementations of a common API, one implementation which is synchronous and the other asynchronous. The API would be designed with an asynchronous interface and the synchronous implementation would probably wrap itself in a promise to fulfill the asynchronous contract of the common API.

除此之外,通常不应该使同步事物异步,因为这只会不必要地使使用它们变得复杂.

Other than that, you should generally not make synchronous things async because it just unnecessarily complicates using them.

我所知道的兑现承诺的最简单方法是:

The simplest way I know of to make it into a promise would be this:

Promise.resolve(path.join(path1, path2)).then(function(path) {
   // use the result here
});

根据您的评论,在 .then()处理程序中,承诺基础结构已经捕获了异常,并将其转换为被拒绝的承诺.所以,如果您有这个:

Per your comments, inside a .then() handler, exceptions are already captured by the promise infrastructure and turned into a rejected promise. So, if you had this:

someAsyncOp().then(function(value) {
   // some other stuff
   // something that causes an exception
   throw new Error("timeout");
}).catch(function(err){
   console.log(err);   // will show timeout
});

然后,该异常已映射为您的诺言拒绝.当然,如果您想在 .then()处理程序内部处理异常(而不是将promise转换为拒绝),则只需在同步操作周围使用传统的try/catch即可:捕获本地异常(与任何其他同步代码相同).但是,如果您希望诺言在 .then()处理程序内部发生异常的情况下拒绝,那么这一切将自动为您完成(诺言的一个非常不错的功能).

Then, that exception is already mapped into a promise rejection for you. Of course, if you want to handle the exception inside of the .then() handler (not turn the promise into a rejection), then you can just use a traditional try/catch around your synchronous operation to catch a local exception (no different than any other synchronous code). But, if you want the promise to reject if there's an exception inside the .then() handler, then that is all done for you automatically (a very nice feature of promises).

这篇关于将同步功能包装成一个承诺的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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