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');

関連する問題