Sözler ile kod yazmayı anlamaya çalışıyorum. Kodumu kontrol et plz. Bu doğru?node.js + request => node.js + bluebird + istek
node.js + isteği:
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 + isteği:
request.getAsync(url)
.spread(function(response, body) {return body;})
.then(JSON.parse)
.then(function(json){console.log(json)})
.catch(function(e){console.error(e)});
Nasıl tepki durumunu kontrol etmek? İlk örnekten mi yoksa daha ilginç bir şey mi kullanmalıyım? durum kodu kontrol etmek
'jsonString' nereden geldi? – thefourtheye
@thefourtheye sry, catch (e) kısmını unutun {...} –