0
我一直在试图理解使用这个简单代码嵌套的承诺。Node.js在forEach循环内的异步承诺在另一个语句内
我调用的两个函数都是异步的,一个给出了整个集合,另一个给出了每个元素的单独信息。
我在做什么错?
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);
});
你为什么认为有什么问题?有问题吗?它是什么,你期望发生什么? –