How to use the co.call function in co

To help you get started, we’ve selected a few co examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github amida-tech / greyscale / backend / app / bologger.js View on Github external
BoLogger.prototype.log = function (data) {
    co.call(this, function* () {
        //data.essence = yield * common.getEssenceId(data.object);
        if (data.object) {
            yield this.init(data.object, data.req);
        }
        if (typeof data.entities === 'object') {
            data.entities = JSON.stringify(data.entities);
        }
        var thunkQuery = (data.req) ? data.req.thunkQuery : thunkify(new Query(config.pgConnect.adminSchema));

        if (data.user) {
            data.userid = (data.user.roleID === 1) ? 0 - data.user.id : data.user.id; // if superuser - then user = -id
        }
        this.extend(data);
        var id = yield thunkQuery(Log.insert(this.data).returning(Log.id));
        return data;
    }).then(function (data) {
github wechaty / wechaty / src / message.js View on Github external
ready() {
    log.silly('Message', 'ready()')

    return co.call(this, function* () {
      const from  = Contact.load(this.obj.from)
      const to    = Contact.load(this.obj.to)
      const room  = this.obj.room ? Room.load(this.obj.room) : null

      if (from) { yield from.ready() }  // Contact from
      if (to)   { yield to.ready() }    // Contact to
      if (room) { yield room.ready() }  // Room member list

      return this         // return this for chain
    }).catch(e => { // Exception
        log.error('Message', 'ready() exception: %s', e)
        console.log(e)
        throw e
    })
  }
github wechaty / wechaty / src / puppet-web.js View on Github external
initBrowser() {
    log.verbose('PuppetWeb', 'initBrowser()')
    const browser = new Browser({
      head: this.head
      , sessionFile: this.profile
    })

    browser.on('dead', Event.onBrowserDead.bind(this))

    // fastUrl is used to open in browser for we can set cookies.
    // backup: 'https://res.wx.qq.com/zh_CN/htmledition/v2/images/icon/ico_loading28a2f7.gif'
    const fastUrl = 'https://wx.qq.com/zh_CN/htmledition/v2/images/webwxgeticon.jpg'

    return co.call(this, function* () {
      yield browser.init()
      yield browser.open(fastUrl)
      yield browser.loadSession()
                  .catch(e => { // fail safe
                   log.verbose('PuppetWeb', 'browser.loadSession(%s) exception: %s', this.profile, e.message || e)
                  })
      yield browser.open()
      return browser // follow func name meaning
    }).catch(e => {
      log.error('PuppetWeb', 'initBrowser() exception: %s', e.message)
      throw e
    })
  }
github koajs / convert / index.js View on Github external
yield Promise.resolve(mw(ctx, function () {
      if (called) {
        // guard against multiple next() calls
        // https://github.com/koajs/compose/blob/4e3e96baf58b817d71bd44a8c0d78bb42623aa95/index.js#L36
        return Promise.reject(new Error('next() called multiple times'))
      }
      called = true
      return co.call(ctx, next)
    }))
  }
github koajs / examples / stream-view / view.js View on Github external
constructor(context) {
    super();

    // render the view on a different loop
    co.call(this, this.render).catch(context.onerror);
  }
github greim / hoxy / src / cycle.js View on Github external
serve(opts) {

    return co.call(this, function*() {

      // First, get all our ducks in a row WRT to
      // options, setting variables, etc.
      let req = this._request
      let resp = this._response
      if (typeof opts === 'string') {
        opts = { path: opts }
      }
      opts = _.extend({
        docroot: pathTools.sep,
        path: url.parse(req.url).pathname,
        strategy: 'replace',
      }, opts)
      let { docroot, path, strategy } = opts
      let headers = _.extend({
        'x-hoxy-static-docroot': docroot,
github fluidsonic / co-flow / lib / helpers / executeRunnables.js View on Github external
function* threadFunction() {
		while (nextIndexToRun < runnables.length) {
			const index = nextIndexToRun++;
			const runnable = runnables[index];

			try {
				resultHandler(index, null, yield runnable);
			}
			catch (error) {
				resultHandler(index, error);
			}
		}
	}

	for (let thread = 1; thread <= threadCount; ++thread) {
		co.call(context, threadFunction);
	}
}
github hyj1991 / v8-analytics / src / HeapSnapshotWorker.js View on Github external
initializeP() {
        return co.call(this, _initialize);

        /**
         * @inner
         */
        function* _initialize() {
            let meta = this._metaNode;

            this._nodeTypeOffset = meta.node_fields.indexOf('type');
            this._nodeNameOffset = meta.node_fields.indexOf('name');
            this._nodeIdOffset = meta.node_fields.indexOf('id');
            this._nodeSelfSizeOffset = meta.node_fields.indexOf('self_size');
            this._nodeEdgeCountOffset = meta.node_fields.indexOf('edge_count');
            this._nodeTraceNodeIdOffset = meta.node_fields.indexOf('trace_node_id');
            this._nodeFieldCount = meta.node_fields.length;

            this._nodeTypes = meta.node_types[this._nodeTypeOffset];
github fluidsonic / co-flow / lib / helpers / executeRunnables.js View on Github external
runnables.forEach(function(runnable) {
		co.call(context, function*() {
			return yield runnable;
		})
		.then(resultHandler.bind(null, index, null), resultHandler.bind(null, index));

		++index;
	});
}
github koajs / convert / index.js View on Github external
const converted = function (ctx, next) {
    return co.call(ctx, mw.call(ctx, createGenerator(next)))
  }
  converted._name = mw._name || mw.name