私はこのようなコードを持っています..いくつかのコンテンツを取得し、すべてがロードされた後、何かをしたいと思います。そのため、Promise.all を使用して、後で解決された値にアクセスします。しかし、それは値を与えていますが、Promise {' content here'} のようです。(console.log を参照してください。) 正規表現を使用して抽出するつもりでしたが、文字列ではなくキーのないオブジェクトの型を確認しますか? なんで?
var request=require('request');
var urls=['someurl','someurl2','someurl3'];
var contents=[];
urls.forEach(function (u) {
contents.push(getContent(u) );
});
Promise.all(contents)
.then(function () {
// All should be loaded by now?
// Promises which are resolved are fulfiled, and values can be accessed later right?
contents.forEach(function (promise) {
var content = Promise.resolve(promise);
console.log(content); // Promise {'test'} ??
console.log(typeof content,Object.keys(content));
// object [] ???
});
}).
catch(function(err) {
//handle error here
});
function getContent(url) {
return new Promise ( function (resolve,reject) {
/** commented and stripped out for testing
request(url, function (err,response, data) {
if(err) {
reject(Error(err));
}
}); **/
resolve("test");
});
}