'await' has no effect on the type of this expression
I was getting this error just because my JSDoc comment was incorrect.
For example I had an async
function that had @returns {string}
:
/**
* Fetch from the swapi API
*
* @param {string} type
* @param {string} id
* @returns {string} JSON
*/
export default async (type, id) => {
console.table([ type, id ]);
const response = await fetch(`https://swapi.co/api/${type}/?page=${id}`);
const json = await response.json();
console.log(json);
return json;
}
I was getting the "'await' has no effect on the type of this expression" warning - but the function looked correct.
However once I changed the JSDoc to @returns {Promise<string>}
then the error disappeared:
/**
* Fetch from the swapi API
*
* @param {string} type
* @param {string} id
* @returns {Promise<string>} JSON
*/
You can also use the @async
hint as the JSDoc documentation suggests:
/** * Download data from the specified URL. * * @async * @function downloadData * @param {string} url - The URL to download from. * @return {Promise<string>} The data from the URL. */
await
is only useful if you use it with a promise, but requests
does not return a promise. It doesn't have a return statement at all, so it's implicitly returning undefined
.
Looks like you meant for it to return a promise, so here's your code with the return added in:
export default (type, id) => {
console.table([ type, id ]);
return fetch(`https://swapi.co/api/${type}/?page=${id}`)
.then(response => response.json())
.then(json => {
console.log(json);
return json;
})
}
p.s, if you prefer to do this using async
/await
, it would look like:
export default async (type, id) => {
console.table([ type, id ]);
const response = await fetch(`https://swapi.co/api/${type}/?page=${id}`);
const json = await response.json();
console.log(json);
return json;
}