我有这个功能:
function AlexaCall(website) {
var awis = require('awis');
var client = awis({
key: keys.alexa.public,
secret: keys.alexa.secret,
});
let responseData = [];
client({
Action: 'UrlInfo',
'UrlInfo.Shared.ResponseGroup': 'Rank',
'UrlInfo.1.Url': 'lupomontero.com',
'UrlInfo.2.Url': 'yahoo.com',
'UrlInfo.3.Url': 'weibo.com',
'UrlInfo.4.Url': 'github.com',
'UrlInfo.5.Url': 'monono.org'
}, (err, data) => {
// res.length === 5
// data is an array with a response object for each domain
data.forEach(function (item) {
//res.write(item.trafficData.dataUrl);
responseData.push(item);
});
console.log(responseData);
callback(null, JSON.stringify(responseData));
});
}
是否可以将其编辑为像这样?
async function PageSpeedCall(website) {
var pagespeedCall = `https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=https://${website}&strategy=mobile&key=${keys.pageSpeed}`;
// second call
var results = await fetch(pagespeedCall);
return await results.json();
}
第一个使用AWIS package。文档非常清楚,但我需要能够像这样调用函数:
async function callAllSites(websites) {
var psRes = [];
var mozRes = [];
for (let website of websites) {
psRes.push(await PageSpeedCall(website));
mozRes.push(await MozCall(website));
}
return await {'ps': psRes, 'moz': mozRes};
// return await [psRes, mozRes];
// return psRes;
}
callAllSites('google.com','stackoverflow.com')
.then(results => {
// console.log(results.ps[3].error)
// console.log(results.error)
// console.log(results.ps[0].formattedResults.ruleResults)
console.log(results);
})
.catch(error => console.log(error))
.then(() => console.log('done'));
我需要传递websites[]
数组,因为这是从视图中传来的内容,用户可以将多个值传递给它。知道如何解决这个问题吗?
答案 0 :(得分:1)
以下是手动宣传的示例。
const awis = require('awis');
async function AlexaCall(website) {
const client = awis({
key: keys.alexa.public,
secret: keys.alexa.secret,
});
let responseData = [];
return new Promise(function (resolve, reject) {
client({
Action: 'UrlInfo',
'UrlInfo.Shared.ResponseGroup': 'Rank',
'UrlInfo.1.Url': 'lupomontero.com',
'UrlInfo.2.Url': 'yahoo.com',
'UrlInfo.3.Url': 'weibo.com',
'UrlInfo.4.Url': 'github.com',
'UrlInfo.5.Url': 'monono.org'
}, (err, data) => {
if (err) {
return reject(err);
}
// res.length === 5
// data is an array with a response object for each domain
data.forEach(function (item) {
//res.write(item.trafficData.dataUrl);
responseData.push(item);
});
console.log(responseData);
resolve(JSON.stringify(responseData));
});
});
}