Ich benutze Knoten js Anfrage, um eine Anfrage an eine andere Website, so Anfrage ist asyncronis Funktion, ich muss einen Code nach allem ausführen getan, aber aus irgendeinem Grund Promise.all() vor ausführt, hier ist der Code:Javascript Promise.all() ausgeführt, bevor die Funktion ausgeführt wird, node.js Anfrage Bibliothek
// in this object I store request's promises
var tempObj = {};
for (var i = self.numberOfPaginations.length; i >= 1; i--) {
tempObj['request'+ i] = request('http://www.somewebsite.com/search/page='+i ,function (err,resp,body) {
// gets urls of listings
if (!err && resp.statusCode == 200) {
var $ = cheerio.load(body);
$('.title').each(function() {
self.urls.push($(this).attr('href'));
});
$('.info a').each(function() {
self.urls.push($(this).attr('href'));
});
// this log out puts the desired result
console.log(self.urls);
}
});
}
// this line of code pushes promises into array
Promise.all(Object.keys(tempObj).map(function (key) {return tempObj[key]})).then(function (argument) {
// this line of code executes before all the work in requests is done , however it should not!
console.log(self.urls);
});
so mein Problem ist, dass die Codezeile in Promise.all() aus irgendeinem Grund vor,
führt
'request()' gibt kein Versprechen zurück. – SLaks
ops, als sollte ich ein Versprechen erstellen? Ich habe nicht bush Erfahrung in Versprechen – Mikail
dann lesen Sie einige [Dokumentation] (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) und sehen, wie Sie gehen –