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

@ -37,10 +37,10 @@ function looseMatch(needle, haystack, type) {
});
}
export function* lookupId(id, type) {
const token = yield spotify.clientCredentialsGrant();
export async function lookupId(id, type) {
const token = await spotify.clientCredentialsGrant();
spotify.setAccessToken(token.body.access_token);
let data = yield spotify[`get${type.charAt(0).toUpperCase()}${type.slice(1)}s`]([id]);
let data = await spotify[`get${type.charAt(0).toUpperCase()}${type.slice(1)}s`]([id]);
data = data.body[`${type}s`][0];
@ -85,8 +85,8 @@ export function* lookupId(id, type) {
return { service: 'spotify' };
}
export function* search(data, original = {}) {
const token = yield spotify.clientCredentialsGrant();
export async function search(data, original = {}) {
const token = await spotify.clientCredentialsGrant();
spotify.setAccessToken(token.body.access_token);
const markets = ['US', 'GB', 'JP', 'BR', 'DE', 'ES'];
@ -109,7 +109,7 @@ export function* search(data, original = {}) {
}
for (const market of markets) { // eslint-disable-line
const response = yield spotify[`search${type.charAt(0).toUpperCase()}${type.slice(1)}s`](query, { market });
const response = await spotify[`search${type.charAt(0).toUpperCase()}${type.slice(1)}s`](query, { market });
const items = response.body[`${type}s`].items;
@ -122,20 +122,20 @@ export function* search(data, original = {}) {
if (match) {
if (type === 'album') {
return yield lookupId(match.id, type);
return await lookupId(match.id, type);
} else if (type === 'track') {
return yield lookupId(match.id, type);
return await lookupId(match.id, type);
}
}
}
return { service: 'spotify' };
}
export function* parseUrl(url) {
export async function parseUrl(url) {
const matches = parse(url).path.match(/\/(album|track)[/]+([A-Za-z0-9]+)/);
if (matches && matches[2]) {
return yield lookupId(matches[2], matches[1]);
return await lookupId(matches[2], matches[1]);
}
throw new Error();
}