関数getStuffは機能しますが、.getメソッドにエラーがあるときはいつでも問題に遭遇します。 getStuff2
を参照してください。スーパーエージェントによる約束
const request = require('superagent');
function getStuff() {
return new Promise(function (resolve, reject) {
request
.get('http://google.com')
.end(function (err, res) {
console.log('getting to end cb');
if (err) reject(err);
resolve(res.body);
})
});
}
function getStuff2() {
return new Promise(function (resolve, reject) {
request
.get('abc')
.end(function (err, res) {
console.log('getting to end cb');
if (err) reject(err);
resolve(res.body);
})
});
}
//works
getStuff().then(function (htmlBody) {
console.log(htmlBody);
}).catch(function(err) {
console.log('err');
});
//doesn't work
getStuff2().then(function (htmlBody) {
console.log(htmlBody);
}).catch(function(err) {
console.log('err');
});
私はキャッチでコンソールログメッセージが表示されていないためたSuperAgentからエラーがgetStuff2
で正しく処理取得されていないように思えます。
'reject()'は '.catch()'の呼び出しを引き起こすはずです。 – robertklep
これは、 'getStuff2()'が終了しないので、ちょうどハングする場所で私と同じ問題があります。 – user3795202