Switch to async/await

This commit is contained in:
Jonathan Cremin 2018-04-14 00:18:48 +01:00
parent 87459b2acc
commit 18021e0ef8
10 changed files with 53 additions and 8837 deletions

View file

@ -5,7 +5,7 @@ import urlMatch from './url';
const apiRoot = 'https://itunes.apple.com';
export function* parseUrl(url) {
export async function parseUrl(url) {
const parsed = parse(url);
const matches = parsed.path.match(/[/]?([/]?[a-z]{2}?)?[/]+album[/]+([^/]+)[/]+([^?]+)/);
const query = querystring.parse(parsed.query);
@ -22,12 +22,12 @@ export function* parseUrl(url) {
}
}
return yield module.exports.lookupId(itunesId, type, matches[1] || 'us');
return await module.exports.lookupId(itunesId, type, matches[1] || 'us');
}
throw new Error();
}
export function* lookupId(possibleId, type, countrycode) {
export async function lookupId(possibleId, type, countrycode) {
let cc = countrycode;
let id = possibleId;
if (String(possibleId).match(/^[a-z]{2}/)) {
@ -39,7 +39,7 @@ export function* lookupId(possibleId, type, countrycode) {
if (cc) {
path = `/${cc}${path}`;
}
const response = yield request.get(apiRoot + path);
const response = await request.get(apiRoot + path);
let result = JSON.parse(response.text);
if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
@ -75,7 +75,7 @@ export function* lookupId(possibleId, type, countrycode) {
}
}
export function* search(data) {
export async function search(data) {
const markets = ['us', 'gb', 'jp', 'br', 'de', 'es'];
let query;
let album;
@ -94,7 +94,7 @@ export function* search(data) {
for (const market of markets) { // eslint-disable-line
const path = `/${market}/search?term=${encodeURIComponent(query)}&media=music&entity=${entity}`;
const response = yield request.get(apiRoot + path);
const response = await request.get(apiRoot + path);
let result = JSON.parse(response.text);
if (!result.results[0]) {
@ -106,7 +106,7 @@ export function* search(data) {
} else if (type === 'track') {
cleanedData.albumName = matches[0].trim();
}
return yield search(cleanedData);
return await search(cleanedData);
}
} else {
result = result.results[0];