0

브라우저에서 선점 형 멀티 태스킹을 사용할 수없고 JavaScript가 본질적으로 단일 스레드이므로 redux-saga와 같은 Redux 미들웨어는 장기 실행 스크립트 대화 상자를 트리거하지 않으면 서 협력 멀티 태스킹을 위해 설계되지 않은 무한 루프를 어떻게 처리합니까?Redux Middleware에서 멀티 태스킹은 어떻게 이루어 집니까?

function* watchSaga() { 
    while (true) { 
     yield take(SOME_REQUEST); 
     // do something 
    } 
} 

편집은

내 문 잘못 "협동 멀티 태스킹을 위해 설계되지 않았습니다." 생성기 함수의 코드는 첫 번째 문자가 일 때까지만 실행됩니다. 표현식입니다.

답변

1

yield은 실제로 키가 컨트롤 인 suspending the current generator function and returning a value to it입니다.

간단한 예 :

function* counter() { 
 
    console.log('Running generator to first yield point'); 
 
    var x = 0; 
 
    do { 
 
    console.log('About to increment and yield control'); 
 
    yield ++x; 
 
    console.log('Running counter to next yield point - x is currently', x); 
 
    } while (true); 
 
} 
 

 
console.log('Instantiating generator'); 
 
var instance = counter(); 
 
console.log('Generator instantiated, calling for the first time'); 
 
console.log('The first entry in counter is', instance.next()); 
 
console.log('The second entry in counter is', instance.next()); 
 
console.log('The third entry in counter is', instance.next()); 
 
console.log('The program continues running');

2

동안이 무한 루프되지 않고, 그 발전기 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators이다.

yield 키워드는 함수를 종료하지만 마지막 실행 된 행을 포함하여 그 상태는 다음 번에 함수가 호출 될 때까지 유지되며 yield 키워드를 다시 볼 때까지 마지막 실행 행 다음의 명령문에서 다시 시작할 때까지 유지됩니다.

관련 문제