简体   繁体   English

通过使用解决或拒绝或Promisify(蓝鸟)进行的承诺

[英]Promisification by using resolve or reject or with Promisify (bluebird)

Hi I have a method in which I use the Promise class provided by bluebird to either resolve or reject based on the result of a third party function. 嗨,我有一种方法,我可以使用蓝鸟提供的Promise类根据第三方函数的结果进行解析或拒绝。 My concern is that the third party function is synchronous and hence could throw an error. 我担心的是第三方功能是同步的,因此可能会引发错误。 Below is my code: 下面是我的代码:

 Authenticator.prototype.createJWTResponse = function(user, secret) { if (user && user.ID) { var expires = moment().add(7, 'days').valueOf(); //encode is third party and synchronous that can throw error var token = jwt.encode({ iss: user.ID, exp: expires }, secret); //throw new Error("thrown by a promise method"); return Promise.resolve({ token: token, expires: expires, user: {} }); } else { return Promise.reject(new Error('Authenticator:createJWTResponse: user object missing or doesnt have ID')); } }; 

One option is that I do not use reject and response in the method and make it a normal callback method which is then promisified by Promise.promisify. 一种选择是,我不在该方法中使用拒绝和响应,而是将其设置为普通的回调方法,然后由Promise.promisify对其进行承诺。 However my question is; 但是我的问题是; if there is a way to use resolve and reject and create a promise in case the third party method throws an exception. 如果有第三方第三方方法抛出异常的情况,则可以使用解决,拒绝和创建承诺的方法。 Another question that I have is; 我还有一个问题是: it is not stated in bluebird documentation; 蓝鸟文档中未对此进行说明; that if one is using resolve and reject in a method; 如果在方法中使用了确定和拒绝; can such a method be promisified? 可以采用这种方法吗? I tried to promisify the above method but it fails to return for some reason & doesnt throw an error; 我试图使上述方法正确化,但是由于某种原因它无法返回,并且不会引发错误; which makes me suspect that if one is using resolve, reject in a method then such a method cannot be promisifed. 这使我怀疑如果使用一种方法在方法中拒绝,那么这种方法就无法实现。

Can someone clarify this for me? 有人可以帮我澄清一下吗? Thanks in advance. 提前致谢。

(Update) (更新)

If I wrap the method in Promise.method; 如果我将方法包装在Promise.method中; bluebird throws a very clear error as follows: 蓝鸟抛出一个非常明显的错误,如下所示:

Possibly unhandled Error: Synchronous Error in otherwise promise returning method 可能未处理的错误:否则为Promise返回方法中的同步错误

Returning a Promise.try from inside this method also throws the same error as in case of Promise.method. 从此方法内部返回Promise.try也会引发与Promise.method相同的错误。

Wrapping the code in the method in a try catch and rejecting on error also throws: 将方法中的代码包装在try catch中,并在错误时拒绝还引发:

Possibly unhandled Error: 可能未处理的错误:

(Solution) (解)

As suggested by Benjamin; 正如本杰明所建议的; taking away all resolve and reject calls from within the method and wrapping it in Promise.method gives the expected result. 从方法中删除所有的解析和拒绝调用,并将其包装在Promise.method中可得到预期的结果。 The final code is as follows: 最终代码如下:

 Authenticator.prototype.createJWTResponse = Promise.method(function(user, secret) { if (user && user.ID) { var expires = moment().add(7, 'days').valueOf(); var token = jwt.encode({ iss: user.ID, exp: expires }, secret); //Note: This is caught by the catch block on this method //throw new Error('sync error thrown'); return { token: token, expires: expires, user: {} } } else { throw new Error('Authenticator:createJWTResponse: user object missing or doesnt have id'); } }); 

Even a callback in not required in this case. 在这种情况下,甚至不需要回调。

First of all .promisify already converts throws to rejections. 首先,.promisify已经将抛出转换为拒绝。

As for normal promisification - the promise constructor is throw safe, you can use Promise.method which is throw safe. 至于正常的承诺-Promise构造函数是安全抛出的,您可以使用Promise.method是安全抛出的。

The reason sync throws are converted to rejects is in order to create a consistent API and so you won't have to .catch and catch all the time. 同步抛出转换为拒绝的原因是为了创建一致的API,因此您不必一直捕获.catch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM