Code Monkey home page Code Monkey logo

Comments (7)

ORESoftware avatar ORESoftware commented on May 27, 2024 2

maybe try this

yield x() || 'whoops'

from co.

ifsnow avatar ifsnow commented on May 27, 2024

Could you tell me why you need yield undefined instead of Promise.resolve(undefined)?
Frequently used? It would be nice if there're an example.

I'm curios about this case.

from co.

keenahn avatar keenahn commented on May 27, 2024

I'm going to vote no on this suggestion. yield undefined is usually an error, and I would want this to throw.

from co.

JWo1F avatar JWo1F commented on May 27, 2024

@ifsnow

function asyncFn(user) {
  if(!user) return;
  const userName = user.name + ' ' + user.lastname;
  if(userName != 'Alex Wolf') return;
  user.fullname = userName;

  return use(user); // returning promise
}

function* main() {
  const user = yield asyncFn(); // error!
}

from co.

ifsnow avatar ifsnow commented on May 27, 2024

@JWo1F I think you can solve it by fixing your code without co's help.

Here is the code I tested.

function use(user) {
    return new Promise(function (resolve, reject) {
        return resolve(user);
    });
};

function asyncFn(user) {
    if(!user || user.name !== 'Alex' || user.lastname !== 'Wolf') {
        return Promise.resolve(undefined);
    }

    user.fullname = user.name + ' ' + user.lastname;

    return use(user);
};

co(function *() {
    const user = {
        name : 'Alex',
        lastname : 'Wolf'
    };

    const processedUser = yield asyncFn(user);
});

I hope this helps.

from co.

clearly avatar clearly commented on May 27, 2024

@ifsnow Last I checked that's more code. I just hit the need for this tonight. I have an entire module abstraction that was not originally built for yielding. I'd like to start introducing the concept of generators into some of those modules.

Without being able to yield undefined I need to restructure/refactor the entire module abstraction across all modules because some modules manipulate data structures but don't actually return anything. Whereas if yield undefined was supported I could very easily introduce generators without restructuring the entire code base.

from co.

ifsnow avatar ifsnow commented on May 27, 2024

@clearly It seems like co doesn't be maintained well. so, I added the way to skip errors for unsupported objects to co-booster that I made. co-booster is a performance tuning version of co. If you're already using co, you can make it work more faster by changing only the module. It's fully compatible with co.

Here is co-booster

If you set skipUnsupportError, co will no longer throw errors for unsupported objects.

co.skipUnsupportError = true;

co(function* () {
  yield undefined;
});

I hope this helps.

from co.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.