이 표현식의 유형에는 영향을 주지 않습니다.
이것에 대해 검색해 보았지만, 특별히 필요한 것은 발견되지 않았습니다.있으면 여기서 공유해 주세요.
다양한 컴포넌트에서 호출할 수 있는 범용 서비스를 만들려고 합니다.외부 소스에 데이터를 요구하는 기능이기 때문에 비동기 기능으로 취급해야 합니다.문제는 편집자가 "ait'는 이 식 유형에 영향을 미치지 않습니다."라는 메시지를 반환한다는 것입니다.그리고 아직 데이터가 없기 때문에 앱이 다운됩니다.
People.js는 서비스 요청을 호출합니다.js
import React, { useEffect, useState } from "react";
import requests from "../services/requests";
export default () => {
// State
const [ people, setPeople ] = useState({ count: null, next: null, previous: null, results: [] });
// Tarefas iniciais
useEffect(() => {
carregarpeople(1);
}, []);
// Carregando os dados da API
const carregarpeople = async (pageIndex) => {
const peopleResponse = await requests("people", pageIndex);
// This line below needs to be executed but it crashes the app since I need to populate it with the data from the function requests
// setPeople(peopleResponse);
}
return (
<div>
{
people.results.length > 0 ? (
<ul>
{
people.results.map(person => <li key = { person.name }>{ person.name }</li>)
}
</ul>
) : <div>Loading...</div>
}
</div>
)
}
이것은 requests.js입니다.API에서 json을 반환합니다.
export default (type, id) => {
console.table([ type, id ]);
fetch(`https://swapi.co/api/${type}/?page=${id}`)
.then(response => response.json())
.then(json => {
console.log(json);
return json;
})}
JSDoc 코멘트가 틀렸기 때문에 이 에러가 발생하고 있었습니다.
예를 들어, 저는async
가지고 있던 기능@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;
}
"wait'는 이 식의 유형에 영향을 주지 않습니다."라는 경고가 표시되었지만 함수는 올바른 것 같습니다.
하지만 JSDoc을 로 변경한 후@returns {Promise<string>}
에러가 사라졌습니다.
/**
* Fetch from the swapi API
*
* @param {string} type
* @param {string} id
* @returns {Promise<string>} JSON
*/
를 사용할 수도 있습니다.@async
힌트는 JSDoc 매뉴얼에 기재되어 있습니다.
/** * Download data from the specified URL. * * @async * @function downloadData * @param {string} url - The URL to download from. * @returns {Promise<string>} The data from the URL. */
await
약속을 가지고 사용해야만 유용하지만requests
는 약속을 반환하지 않습니다.Return 스테이트먼트가 전혀 없기 때문에 암묵적으로 Return 스테이트먼트를 하고 있습니다.undefined
.
약속을 돌려주려고 했던 것 같습니다.반품을 추가한 코드를 다음에 나타냅니다.
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, 만약 당신이 이것을 하고 싶다면async
/await
다음과 같습니다.
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;
}
만약 당신이 이것을 타이프스크립트와 함께 받는다면, 그것은 아마도 당신이 그것을 돌려주지 않기 때문일 것이다.Promise
예를 들어 다음과 같습니다.
❌외부:
async delPerson (id: string): Partial<Person> {
return await this.personModel.findByIdAndRemove(id);
}
deletedPerson = await this.personService.delPerson(body._id);
// in above line typescript thinks that he is awaiting for something which is not a promise
✅정답:
async delPerson (id: string): Promise<Partial<Person>> {
return await this.personModel.findByIdAndRemove(id);
}
deletedPerson = await this.personService.delPerson(body._id);
해결책을 찾았어요이 제안은 기다린 후에 잘못된 개체를 넣었기 때문에 나타납니다.이를 완전히 삭제하려면 wait 키워드 뒤에 약속(괄호 없음) 또는 약속을 반환하는 함수를 입력합니다.
내 경우 이 문제는 순전히 메서드의 js-doc과 관련되어 있습니다.
내 메서드에는 이미 비동기 수식어가 있습니다.
오리지널:
/**
* bla bla
* @return {String} bla bla bla
*/
수정:
/**
* bla bla
* @return {Promise<String>} bla bla bla
*/
언급URL : https://stackoverflow.com/questions/60368017/await-has-no-effect-on-the-type-of-this-expression
'source' 카테고리의 다른 글
Angular.js를 통한 세션 유지 (0) | 2023.04.05 |
---|---|
jQuery 팝업에서 WordPress 로그인 - jQuery Ajax 로그인을 검증하는 방법 (0) | 2023.04.05 |
webpack.config.webpack 파일에서 "webpack build failed: unknown word"가 실패함 (0) | 2023.04.05 |
다른 모든 JS 아래의 Wordpress 플러그인 스크립트를 큐에 넣습니다. (0) | 2023.04.05 |
wordpress 고급 사용자 지정 필드 Google map api 키 (0) | 2023.04.05 |