Last active
June 10, 2018 18:56
-
-
Save tho-graf/e9af879d8091432a1a11db933db1acc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getPackageNames(input) { | |
// you don't need the global array and mutate it later. The map method will create a new array for you ;) | |
if (!input) { | |
return Promise.resolve({ options: [] }); | |
} | |
return fetch(`https://api.npms.io/v2/search?q=${input}`) | |
.then(response => response.json()) | |
// the map method will | |
// 1. create a new array | |
// 2. call the function for every entry of the array | |
// 3. stores the result of the function at the same index | |
// 4. returns the new array with the transformed values | |
// | |
// This will be less error prone, because you don't have any global state and most of the logic will be | |
// handled by the map method. You just have to describe *what* you want to do with every value. | |
.then(json => ({options: json.results.map(item => item.package)})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment