[英]How to promisify correctly JSON.parse method with bluebird
I'm trying to promisify JSON.parse
method but unfortunately without any luck. 我试图宣传
JSON.parse
方法,但遗憾的是没有任何运气。 This is my attempt: 这是我的尝试:
Promise.promisify(JSON.parse, JSON)(data).then((result: any) => {...
but I get the following error 但是我收到以下错误
Unhandled rejection Error: object
Promise.promisify
is thought for asynchronous functions that take a callback function. Promise.promisify
被认为是采用回调函数的异步函数。 JSON.parse
is no such function, so you cannot use promisify
here. JSON.parse
没有这样的功能,所以你不能在这里使用promisify
。
If you want to create a promise-returning function from a function that might throw
synchronously, Promise.method
is the way to go: 如果你想从一个可能同步
throw
的函数创建一个promise- Promise.method
函数, Promise.method
是要走的路:
var parseAsync = Promise.method(JSON.parse);
…
parseAsync(data).then(…);
Alternatively, you will just want to use Promise.resolve
to start your chain: 或者,您只想使用
Promise.resolve
来启动您的链:
Promise.resolve(data).then(JSON.parse).then(…);
First of all, JSON.parse
is not an asynchronous function. 首先,
JSON.parse
不是异步函数。 So, don't try to promisify it. 所以,不要试图宣传它。
Because I want to create a chain of promises where JSON.parse stand at the top
因为我想创建一个承诺链,其中JSON.parse站在顶部
Then, simply create a Promise resolved with the parsed JSON object, like this 然后,简单地创建一个使用解析的JSON对象解析的Promise,就像这样
Promise.resolve(JSON.parse(data))
.then(...)
Now, to your actual question, you are getting the error, 现在,根据您的实际问题,您收到错误,
Unhandled rejection Error: object
because, if your chain of promises is rejected, you are not handling it. 因为,如果你的承诺链被拒绝,你就不会处理它。 So, don't forget to attach a catch handler, like this
所以,不要忘记附加一个catch处理程序,就像这样
Promise.resolve(JSON.parse(data))
.then(...)
.catch(...)
READ THIS There is a problem with the approach I have shown here, as pointed out by Bergi, in the comments. 阅读本文我在这里展示的方法存在问题,正如Bergi在评论中所指出的那样。 If the
JSON.parse
call fails, then the error will be thrown synchronously and you may have to write try...catch
around the Promise
code. 如果
JSON.parse
调用失败,则错误将同步抛出,您可能必须编写try...catch
绕Promise
代码。 Instead, one would write it as Bergi suggested in his answer , to create a Promise object with just the data, and then do JSON.parse
on that Promise chain. 相反,人们会像Bergi在他的回答中所建议的那样编写它,只用数据创建一个Promise对象,然后在Promise链上做
JSON.parse
。
Late to the party, but I can totally understand why you might want a promisified JSON parse method which never throws exceptions. 迟到了,但我完全理解为什么你可能想要一个永远不会抛出异常的promisified JSON解析方法。 If for nothing else, then to remove boilerplate try/catch-handling from your code.
如果没有别的,那么从你的代码中删除样板try / catch处理。 Also, I see no reason why synchronous behavior shouldn't be wrapped in promises.
此外,我认为没有理由不应将同步行为包含在promises中。 So here:
所以在这里:
function promisedParseJSON(json) {
return new Promise((resolve, reject) => {
try {
resolve(JSON.parse(json))
} catch (e) {
reject(e)
}
})
}
Usage, eg: 用法,例如:
fetch('/my-json-doc-as-string')
.then(promisedParseJSON)
.then(carryOn)
.catch(dealWithIt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.