I'm trying to use rxjs in conjunction with babeljs to create an async generator function that yields when next
is called, throws when error
is called, and finishes when complete
is called. The problem I have with this is that I can't yield from a callback.
I can await
a Promise to handle the return/throw requirement.
async function *getData( observable ) {
await new Promise( ( resolve, reject ) => {
observable.subscribe( {
next( data ) {
yield data; // can't yield here
},
error( err ) {
reject( err );
},
complete() {
resolve();
}
} );
} );
}
( async function example() {
for await( const data of getData( foo ) ) {
console.log( 'data received' );
}
console.log( 'done' );
}() );
Is this possible?