Giter VIP home page Giter VIP logo

Comments (6)

navaru avatar navaru commented on May 18, 2024

What did you expect to get by doing that? I'm missing the functionality

from co.

Qard avatar Qard commented on May 18, 2024

Assume foo has some other properties in it that are thunks to get later values. The Date objects in there will get reset in the yield.

Here's a more verbose example;

var assert = require('assert')
var co = require('co')

co(function* () {
  var foo = {
    other: function (done) {
      setTimeout(function () {
        done(null, 'delayed value')
      }, 100)
    },
    now: new Date
  }

  var bar = yield foo

  assert(bar.other === 'delayed value')
  assert(foo.now.getTime() === bar.now.getTime())
})()

foo.now and bar.now should contain the same Date object, so calling getTime() on either should return the same value. This is not the case currently. foo.now.getTime() will be equal to the time the foo object was created, while bar.now.getTime() will be equal to the time the yield resolved. The value gets reset.

from co.

navaru avatar navaru commented on May 18, 2024

I was looking at the tests and understand your issue:

it('should ignore non-thunkable properties', function(done){
    co(function *(){
      var res = yield {
        name: { first: 'tobi' },
        age: 2,
        address: read('index.js', 'utf8')
      };

      res.name.should.eql({ first: 'tobi' });
      res.age.should.equal(2);
      res.address.should.include('exports');
    })(done);
  })

from co.

Qard avatar Qard commented on May 18, 2024

Yeah, that stuff works fine. It's the Date object specifically that doesn't work. I'm guessing co needs some extra stuff to detect instances of things and not run them through the recursive object yielding stuff or something. Not sure exactly how that'd work.

from co.

navaru avatar navaru commented on May 18, 2024

The behavior is correct, in your example foo.now and bar.now have the same value.

Because Date is an object and not a primitive, it goes through objectToThunk, and a new Date object is created, so the assertion is invalid.

function objectToThunk(obj){
  var ctx = this;

   return function(done){
     var keys = Object.keys(obj);
     var pending = keys.length;
     var results = new obj.constructor();    <--  here is created a new Date object
     var finished;
...

I don't know if this is an issue, because the desired functionality is correct.

from co.

Qard avatar Qard commented on May 18, 2024

Breaking any object that normally takes arguments to initialize is correct behaviour?

It should probably do a obj.constructor === Object check or something before running that.

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.