behavioral
behavioral copied to clipboard
how can i use async api in b-thread?
in your example, all calls in b-threads are sync functions. but in a real case, I will use some async functions (eg. HTTP request) how can I do that?
Sorry for the late reply @stormslowly
Essentially you can use promises in the function generators and dynamically create another b-thread that requests an event:
bp.addBThread('foo', ++priority, function *() {
yield { wait: 'addHot' }
callSomePromise()
.then(() => bp.addBThread('bar', ++priority, function *() {
yield { request: 'promiseResolved' }
}))
});
// another b-thread can wait for 'promiseResolved'
function *() {
yield { wait: 'promiseResolved' }
// do something else
}