0
私はこの簡単なコードで約束を理解しようとしています。Node.js別のthen文のforEachループ内で非同期に約束します
私が呼んでいる両方の関数は非同期であり、1つはコレクション全体を与え、もう1つは要素のそれぞれの個別情報を与えます。
私は間違っていますか?
const PirateBay = require ('thepiratebay');
var os = require ('os');
var sys = require('util');
var util = require('util');
var cfg = require('./config/appconf.js');
var mysql = require('mysql');
var Torrent = require('./models/torrent.js');
var parseTorrent = require('parse-torrent')
var async = require('async');
function saveResults (results) {
console.log("Save Results");
var cTorrents = [];
for (var key in results) {
var t =results[key];
var torrent = new Torrent()
torrent.id = t.id;
torrent.name = t.name;
torrent.size = t.size;
torrent.seeders = t.seeders;
torrent.leechers = t.leechers;
torrent.verified = t.verified;
torrent.uploader = t.uploader;
torrent.category = t.category.name;
torrent.description = t.description;
torrent.subcategory = t.subcategory.name;
var r = parseTorrent (t.magnetLink);
torrent.announce = r.announce;
torrent.hash = r.infoHash;
cTorrents.push (torrent);
}
return cTorrents;
}
PirateBay
.recentTorrents()
.then(function(results){
var lTorrents = saveResults(results);
async.each (lTorrents,function (t,next){
await PirateBay
.getTorrent(t.id)
.then(function (err, doc){
console.log(doc.description);
t.doc = doc.description;
next();
});
},function (err) {
console.log ("WHNEEEEE");
console.log(lTorrents);
});
console.log(lTorrents);
})
.catch (function (err){
console.log(err);
});
なぜあなたは何か問題があると思いますか?問題はありますか?それは何ですか、代わりに何が起こると思いますか? –