Questions tagged [co]

The Co javascript library for generator based flow-control of asynchronous tasks

Co is a javascript library for executing generators that yield asynchronous tasks, so that asynchronous code can be written in a more concise manner.

114 questions
1
vote
0 answers

co generator yield not working with mongoclient

const Db = require('mongodb').Db; const Server = require('mongodb').Server; const co = require('co'); const v1Db = new Db('v2-staging', new Server('localhost', 27017)); function* migrateBatch() { const collectionV1Uploadfile = yield…
Shishir Sonekar
  • 410
  • 1
  • 3
  • 15
1
vote
0 answers

understanding co library and javascript generator with example

I am trying to understanding co libary - https://github.com/tj/co and using this library with promises. I am using following code as example - const co = require('co'); function foo(x) { return new Promise(function (resolve) { …
daljit
  • 1,256
  • 1
  • 11
  • 30
1
vote
1 answer

How to use generators in a proper way with loops

Let's say that we have some data in an array and we need to save each array item in a separate document in mongodb Here is a code how do I try to do that: const co = require('co'); const Model = new require('./mongoose').Schema({...}); const data…
Zurab
  • 1,411
  • 3
  • 17
  • 39
1
vote
1 answer

Synchronous Like Programming in NodeJS / Express with Yield

I'm having a really hard time with Node Asynchronous operation (coming from a PHP background). I know that you can nest callbacks but that can get out of hand really quickly. Here is a basic example I want to solve synchronously (I know it might be…
Jordash
  • 2,926
  • 8
  • 38
  • 77
1
vote
4 answers

Console.log shows array but can't be returned

I'm new to working with promises (I'm using 'co' in node) so I'm not entirely sure what's failing with this code: function* excelToJSON(excelFileNames) { var jsonData = []; for (let index = 0; index < excelFileNames.length; index++) { …
Vonmood
  • 47
  • 7
1
vote
1 answer

How to return thenable from wrapped generator function in Javascript?

I have such a code in my project: co(function *asyncFn() { let browser = yield someAsyncOperation(); return browser; }).then(browser => browser.exit()); As expected, this doesn't work since browser object has method then. I wonder if there…
James Akwuh
  • 2,169
  • 1
  • 23
  • 25
1
vote
2 answers

promises with mongoose and es6 not working as expected

I have the following code which creates an array of promises to save some numbers, then it yields the promises (using co library) and prints out the results. What I don't understand, however, is that when it prints the output, it prints the same…
dcp
  • 54,410
  • 22
  • 144
  • 164
1
vote
2 answers

Why is node.js generator not working as expected?

Given the following two code snippets, why is the transaction object visible in the working case but not visible in the other case? Working case: return db.transaction(function(transaction) { return co(function*() { // transaction is visible,…
Scholle
  • 1,521
  • 2
  • 23
  • 44
1
vote
2 answers

How can I catch Exception using co module on Node.js?

I am coding using co module on sail framework. I want to catch InvalidError but error log says 'undefined'. How can I fix this code? Co module can't catch ErrorType specification?? detail: function (req, res) { co(function *() { let errors =…
1
vote
2 answers

Using co coroutine library client-side in browser?

How can I use co coroutine library in a client application in the browser?
LongHike
  • 4,016
  • 4
  • 37
  • 76
1
vote
1 answer

koajs multipart form post, extract zip before send reply

i use koajs for my interface i have a form with 1 to x attached zip as files how can we do a loop on these files to get them extracted before koa send the reply :/ ? here s what i tried (don t understand very well so could be very bad :p…
1
vote
2 answers

converting convention node.js function with callback to generators and yield

I am new to koa.js and liked it very much, started a project with it. i need to use twilio for sms sending. most calls for twilio package follow this structure. public.get('/najam', function *(){ this.body = "hello from najam"; //yeild…
Najam Awan
  • 1,113
  • 3
  • 13
  • 30
1
vote
1 answer

Use promise to wait for database operation

I have a function that calls another function, which connects to a database to get some value. Until the execution is completed, I cannot proceed further as that value is to used for another transaction on the database. I am using yield to…
faizanjehangir
  • 2,771
  • 6
  • 45
  • 83
1
vote
2 answers

How do I get away from using the `self = this` hack when using ES6 classes and generator functions?

I have tried to use the explicit .bind(this) and that did not work. I also know arrow functions dont work here. 'use strict'; const co = require('co'); class ServiceDemo { constructor(repository, config, loggingService) { …
CSharpAtl
  • 7,374
  • 8
  • 39
  • 53
1
vote
1 answer

Create generator from event emitter

I have a function that does something asynchronously, e.g., const doSomething = () => { request(url) .pipe(hasher) .on('finish', () => { // "return" only here return hasher.read(); }); }); I would now like to "wait" in the function…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249