Kayıtlar

redux-saga etiketine sahip yayınlar gösteriliyor

The problem with async generators

Let’s imagine that we want to manage cancellation of an async operation using generator functions in javascript. When a secondary event occurs, we can listen for it and force the generator function to return by calling its return method and handle cleanup in the finally block, which will always run before a generator returns. Say we have the following imaginary async generator, which will wait for a second and yield; var generator = async function*() { try { yield await new Promise((r) => { setTimeout(r, 1000) }).then(() => 'Waited for 1 sec.') // More yields... } finally { // Do cleanup here. e.g. abort an XHR or invoke a // cancellation on some ongoing operation console.log('Did cleanup.') }} Then we want to run such a generator to completion in a loop. One way to do this is using an asynchronous for await…of loop. Let’s say we call return before the async operation in the generator is complete. va...