behavioral icon indicating copy to clipboard operation
behavioral copied to clipboard

how can i use async api in b-thread?

Open stormslowly opened this issue 7 years ago • 1 comments

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?

stormslowly avatar Feb 13 '19 03:02 stormslowly

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
}

lmatteis avatar Apr 18 '19 13:04 lmatteis