admin管理员组文章数量:1287892
I'm trying to understand how to write code with promises. Check my code plz. This is right?
Node.js + request:
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonpData = body;
var json;
try {
json = JSON.parse(jsonpData);
} catch (e) {
var startPos = jsonpData.indexOf('({');
var endPos = jsonpData.indexOf('})');
var jsonString = jsonpData.substring(startPos+1, endPos+1);
json = JSON.parse(jsonString);
}
callback(null, json);
} else {
callback(error);
}
});
Node.js + bluebird + request:
request.getAsync(url)
.spread(function(response, body) {return body;})
.then(JSON.parse)
.then(function(json){console.log(json)})
.catch(function(e){console.error(e)});
How to check response status? I should use if from first example or something more interesting?
I'm trying to understand how to write code with promises. Check my code plz. This is right?
Node.js + request:
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonpData = body;
var json;
try {
json = JSON.parse(jsonpData);
} catch (e) {
var startPos = jsonpData.indexOf('({');
var endPos = jsonpData.indexOf('})');
var jsonString = jsonpData.substring(startPos+1, endPos+1);
json = JSON.parse(jsonString);
}
callback(null, json);
} else {
callback(error);
}
});
Node.js + bluebird + request:
request.getAsync(url)
.spread(function(response, body) {return body;})
.then(JSON.parse)
.then(function(json){console.log(json)})
.catch(function(e){console.error(e)});
How to check response status? I should use if from first example or something more interesting?
Share Improve this question edited Jun 28, 2015 at 10:04 Sergey P. asked Jun 28, 2015 at 9:20 Sergey P.Sergey P. 1941 silver badge12 bronze badges 2-
2
Where did
jsonString
e from? – thefourtheye Commented Jun 28, 2015 at 9:23 - @thefourtheye sry, forget part for catch(e) {...} – Sergey P. Commented Jun 28, 2015 at 10:05
2 Answers
Reset to default 11You can simply check if the response.statusCode
is not 200 in the spread
handler and throw an Error
from that, so that the catch
handler will take care of it. You can implement it like this
var request = require('bluebird').promisifyAll(require('request'), {multiArgs: true});
request.getAsync(url).spread(function (response, body) {
if (response.statusCode != 200)
throw new Error('Unsuccessful attempt. Code: ' + response.statusCode);
return JSON.parse(body);
}).then(console.log).catch(console.error);
And if you notice, we return the parsed JSON from the spread
handler, because JSON.parse
is not an async function, so we don't have to do it in a separate then
handler.
One way to check the status code:
.spread(function(response, body) {
if (response.statusCode !== 200) {
throw new Error('Unexpected status code');
}
return body;
})
本文标签: javascriptnodejsrequest gt nodejsbluebirdrequestStack Overflow
版权声明:本文标题:javascript - node.js + request => node.js + bluebird + request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741324468a2372395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论