Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make primitive types yieldable #293

Open
tzlAsTom opened this issue Sep 5, 2016 · 1 comment
Open

Make primitive types yieldable #293

tzlAsTom opened this issue Sep 5, 2016 · 1 comment

Comments

@tzlAsTom
Copy link

tzlAsTom commented Sep 5, 2016

const co = require('co');


function delay(seconds){
    if(!(seconds > 0)) return 0;

    return new Promise( (resolve, reject) => {
        setTimeout( () => resolve(seconds), seconds * 1000);
    });
};

return co(function* (){
    let rtn;
    rtn = yield delay(1);
    console.log('rtn1', rtn);
    rtn = yield delay(0);
    console.log('rtn0', rtn);
    return rtn;
}).then( (result) => {
    console.log('result', result);
}).catch( (error) => {
    console.error('error', error);
});

Will get:

[tong@localhost test]$ node genTest.js
rtn1 1
error TypeError: You may only yield a function, promise, generator, array, or object, but the following object was passed: "0"

Expect:

[tong@localhost test]$ node genTest.js
rtn1 1
rtn0 0
result 0
@cmazakas
Copy link

This should be: if(!(seconds > 0)) return Promise.resolve(0);

Mixing return types in an un-typed language like JS is just asking for maintainability and debugging nightmares. By returning a synchronous Promise, you keep the structure of the code relatively the same and don't introduce any sort of type-handling (was I returned a Promise or a primitive?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants