Browse Source

Use Promise.all and manually fetch app details

master
KaKi87 1 year ago
parent
commit
86968491cb
1 changed files with 13 additions and 15 deletions
  1. +13
    -15
      gplay.js

+ 13
- 15
gplay.js View File

@@ -9,23 +9,21 @@ module.exports = {
GPlay.search({
term: query.name,
num: query.number,
price: query.price,
fullDetail: true // 1 more request per app to get app details (excepted permissions)
price: query.price
}).then(res => {
// If no result, no additional request required
if(res.length === 0) return callback(res);
let i = 0;
res.forEach((app, index) => {
app.index = index;
// 1 more request per app to get permissions
GPlay.permissions({ appId: app.appId })
.then(permissionsList => {
app.permissions = permissionsList;
i++;
if(i === res.length)
callback(res);
})
.catch(err => console.log(err));
});
// Second request : get apps details
Promise.all(res.map(app => GPlay.app({ appId: app.appId })))
.then(apps => Promise.all(apps.map((app, index) => new Promise((resolve, reject) => {
// Index apps
app.index = index;
// Third request : get apps permissions
GPlay.permissions({ appId: app.appId }).then(permissions => {
app.permissions = permissions;
resolve();
}).catch(reject);
}))).then(() => callback(apps)));
})
.catch(err => console.log(err));
}


Loading…
Cancel
Save