如何promisify与蓝鸟正确JSON.parse方法 [英] How to promisify correctly JSON.parse method with bluebird
问题描述
我想promisify JSON.parse
方法,但遗憾的是没有任何运气。这是我尝试:
Promise.promisify(JSON.parse,JSON)(数据)。然后((结果:任意)=> {...
但我得到了以下错误
未处理拒绝错误:对象
首先, JSON.parse
不是一个异步函数。所以,不要尝试的 promisify 的吧。
由于我想创建承诺链,其中JSON.parse站在顶部
块引用>然后,只需创建一个解析的JSON对象解决的承诺,像这样
Promise.resolve(JSON.parse(数据))
。然后(...)现在,你的实际问题,你所得到的错误,
未处理拒绝错误:对象
因为,如果你的诺言链被拒绝,你不处理它。所以,不要忘记附上catch处理,像这样
Promise.resolve(JSON.parse(数据))
。然后(...)
。抓住(...)I'm trying to promisify
JSON.parse
method but unfortunately without any luck. This is my attempt:Promise.promisify(JSON.parse, JSON)(data).then((result: any) => {...
but I get the following error
Unhandled rejection Error: object
解决方案First of all,
JSON.parse
is not an asynchronous function. So, don't try to promisify it.
Because I want to create a chain of promises where JSON.parse stand at the top
Then, simply create a Promise resolved with the parsed JSON object, like this
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
Promise.resolve(JSON.parse(data)) .then(...) .catch(...)
这篇关于如何promisify与蓝鸟正确JSON.parse方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!