나는 Typescript에 더 신선하고 사이트에서 배우는 동안 for-await-of를 사용하여 비동기 반복에 yield를 사용할 수 있음을 알게되었습니다. 다음은 Javascript의 기능입니다. Typescript 클래스에서 사용하는 방법을 도와주세요. 아래 코드를 작성할 때 TS1163 : A 'yield'표현식은 생성기 본문에서만 허용됩니다. Typescript 클래스에 아래 코드를 작성하고 싶습니다 .
https://blog.bitsrc.io/keep-your-promises-in-typescript-using-async-await-7bdc57041308 .
function* numbers() {
let index = 1;
while(true) {
yield index;
index = index + 1;
if (index > 10) {
break;
}
}
}
function gilad() {
for (const num of numbers()) {
console.log(num);
}
}
gilad();
나는 또한 Typescript 클래스에서 작성하려고 시도했지만 컴파일 문제가 발생합니다.
public getValues(): number {
let index = 1;
while(true) {
yield index;
index = index + 1;
if (index > 10) {
break;
}
}
}
*
메서드에서 토큰을 넣어야합니다 .
class X {
public *getValues() { // you can put the return type Generator<number>, but it is ot necessary as ts will infer
let index = 1;
while(true) {
yield index;
index = index + 1;
if (index > 10) {
break;
}
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다