0

What is the best way to yield until success of a Promise using the js-csp library?

For example:

import * as csp from 'js-csp'

const goRoutine = function*()
{
    while (true)
    {
        const payload = yield csp.take(CHANNELS.SAVE_TO_SERVER);
        yield api.save(payload); // returns promise
        console.log("saved"); // should log after the promise was successful
    }
}

csp.go(goRoutine);

This is possible to do using the co library (see this answer) and was hoping that similar functionality would be available in js-csp. If not, what is the ideal way to do this?

Normally we can do this by calling it.next(result) on our generator when the Promise has succeeded, but I don't see any way to gain access to this iterator from within our CSP 'go routine' in order to do this.

sookie
  • 2,437
  • 3
  • 29
  • 48
  • Doesn't look like it. https://github.com/ubolonton/js-csp/issues/13 – Bergi Jan 19 '18 at 17:43
  • @Bergi Seems so. Looks like there are talks around implementing something for it in the near future. The solution I went with was to just create a 'return' channel that the promise will use to put the result on (after success). My original generator just yields until the value comes in on that channel. Not ideal but it works – sookie Jan 24 '18 at 12:04

0 Answers0