Skip to content

Instantly share code, notes, and snippets.

View ybogdanov's full-sized avatar

Yuriy Bogdanov ybogdanov

View GitHub Profile
@ybogdanov
ybogdanov / rimraf-sync.js
Created August 9, 2011 21:51
Rimraf implementation using node-sync (node-fibers abstraction lib)
var path = require('path')
, fs = require('fs')
, Sync = require('sync')
// Return a future which just pauses for a certain amount of time
function realish (p) {
return path.resolve(path.dirname(fs.readlink.sync(fs, p)))
}
@ybogdanov
ybogdanov / gist:1010061
Created June 6, 2011 10:44
Using ES6 coroutines for synchronous yielding of asynchronous stuff
function someAsyncFunction(param, callback) {
setTimeout(function(){
callback(null, param); // result
}, 100)
}
Function.prototype.sync = function(param) {
this(param, function(e, result) {
Fiber.run(result); // substitute result instead of yield, resume current Fiber
@ybogdanov
ybogdanov / sync_integration.js
Created March 25, 2011 10:21
Example that shows how you can easily integrate node-sync to your app without refactoring
someObject = {
myNewMethod : function(a, b) // <-- no callback
{
console.log(Fiber.current); // Inside of fiber
if (a == 1)
throw "'a' cannot be 1"; // we can use throw here
@ybogdanov
ybogdanov / seq_vs_sync.js
Created March 25, 2011 10:04
Seq library example VS Sync example
/**
* Seq
*/
var fs = require('fs');
var exec = require('child_process').exec;
var Seq = require('seq');
Seq()
.seq(function () {
exec('whoami', this)
@ybogdanov
ybogdanov / fibers.js
Created March 24, 2011 09:28
fibers integration
/**
* Some existing external lib which you don't want to touch
*/
var SomeModule = {
someAsyncFunction : function(callback)
{
process.nextTick(function(){
callback(null, 'result');
})