Using this code as a reference I tried making a function which takes words as input and chops it into 32-word pieces and returns a list of the different search data:
async function singleSearch(q, cx, auth) {
const res = await customsearch.cse.list({
cx: cx,
q: q,
auth: auth,
});
return res.data;
};
function search(words, apikey=process.argv[2], engineid=process.argv[3]){
const limit = 32; //32 word limit on google search
var searchQueries = [];
var len = Math.ceil(words.length/limit);
for(var i = 0; i < len; i++){
searchQueries.push( words.slice(i*limit, (i+1)*limit).join(" ") )
};
var searches = []
for(var i = 0; i < searchQueries.length; i++){
searches.push(singleSearch(searchQueries[i], engineid, apikey).catch(console.error))
};
return searches
}
a = search("Do I understand it correctly that anotherCall() will be called only when someCall() is completed? What is the most elegant way of calling them in parallel?".split(" "));
console.log(a)
This approach doesn't work and returns a Promise { <pending> }
, and no matter how many await/async
's or Promise.all()
's I try, I can't get it to work. What is the correct way to call the API in this case?
Expected Output:
[
{
kind: 'customsearch#search',
url: {
type: 'application/json',
template: 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json'},
...
},
...
]