javascript 如何使用 bluebird 正确承诺 JSON.parse 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32309392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to promisify correctly JSON.parse method with bluebird
提问by Mazzy
I'm trying to promisify JSON.parse
method but unfortunately without any luck. This is my attempt:
我试图承诺JSON.parse
方法,但不幸的是没有任何运气。这是我的尝试:
Promise.promisify(JSON.parse, JSON)(data).then((result: any) => {...
but I get the following error
但我收到以下错误
Unhandled rejection Error: object
回答by Bergi
Promise.promisify
is thought for asynchronous functions that take a callback function. JSON.parse
is no such function, so you cannot use promisify
here.
Promise.promisify
被认为是采用回调函数的异步函数。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.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(…);
回答by mtkopone
Late to the party, but I can totally understand why you might want a promisified JSON parse method which never throws exceptions. If for nothing else, then to remove boilerplate try/catch-handling from your code. Also, I see no reason why synchronous behavior shouldn't be wrapped in promises. So here:
迟到了,但我完全理解为什么你可能想要一个从不抛出异常的承诺的 JSON 解析方法。如果没有别的,那么从您的代码中删除样板 try/catch-handling。此外,我认为没有理由不应该将同步行为包装在 Promise 中。所以在这里:
function promisedParseJSON(json) {
return new Promise((resolve, reject) => {
try {
resolve(JSON.parse(json))
} catch (e) {
reject(e)
}
})
}
Usage, e.g:
用法,例如:
fetch('/my-json-doc-as-string')
.then(promisedParseJSON)
.then(carryOn)
.catch(dealWithIt)
回答by thefourtheye
First of all, JSON.parse
is not an asynchronous function. So, don't try to promisifyit.
首先,JSON.parse
不是异步函数。所以,不要试图承诺它。
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 THISThere is a problem with the approach I have shown here, as pointed out by Bergi, in the comments. 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. 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 在评论中指出的那样,我在这里展示的方法存在问题。如果JSON.parse
调用失败,则该错误将被同步引发,你可能得写try...catch
周围的Promise
代码。相反,人们会像 Bergi 在他的回答中建议的那样编写它,创建一个仅包含数据的 Promise 对象,然后JSON.parse
在该 Promise 链上执行。