asciify icon indicating copy to clipboard operation
asciify copied to clipboard

Can we have a sync version while using as node module?

Open vishr opened this issue 10 years ago • 5 comments

vishr avatar Nov 21 '13 16:11 vishr

Do you really really need it? asciify loads a file off disk to get the font, so making that sync would tie up the event loop while the disk IO occurred. It's usually a bad idea to force node to do things sync style, so I'm keen to avoid endorsing that, and instead keep this module as simple as possible.

olizilla avatar Nov 21 '13 17:11 olizilla

The ask is for a sync version along with async and the reason is that my program exists without display the ascii text if there is an error or something similar. I am aware that I can do it in a callback but just thought that it would be nice to have a sync version like we have in the core node library for reading file? I am fine if it's going against the design.

vishr avatar Nov 21 '13 17:11 vishr

Hi Oli,

I kind of also have a similar need. Though there may be another method to do this (please feel free to suggest as I am a newbie with yo/js). However I am not able to figure this out.

I need to get the return value back to main program. So essentially trying to do this:

asciify(' ' + _.capitalize(this.name), {font:'slant'}, function (err, res) {
                 this.banner = res;
});
writeToFile('output.txt', this.banner);

I need the banner value captured so it can be written to a file. This is for a 'yo' generator. Just to add, there is a lot of logic/code after the capture of banner value so I cannot simply add that to callback.

How do I do this?

Thanks Amit

amitjindal avatar Jun 05 '15 11:06 amitjindal

@amitjindal did you see https://github.com/olizilla/grunt-asciify

it sets a property with the asciified text when it's available: https://github.com/olizilla/grunt-asciify/blob/master/tasks/asciify.js#L25

...and grunt handles the async issue by providing a done callback function, so the next task waits on asciify to complete, so we can assume the property is available to subsequent tasks.

If that's not relevant, then you could take a peek at one of the other yo generators using it: https://www.npmjs.com/browse/depended/asciify

and see how they do.

olizilla avatar Jun 05 '15 14:06 olizilla

Thanks for the tip. After a few trials I figured it out. Very weird syntax. But now it works.

MyClass.prototype.createBanner = function createBanner(text) {
    var cb = this.async();
    asciify(text, {font:'slant'}, function (err, res) {
        this.banner = res;
        cb();
    }.bind(this));
};

Later in the code:

this.createBanner(' ' + _.capitalize(this.baseName));
console.log(this.banner);

amitjindal avatar Jun 07 '15 18:06 amitjindal