Skip to content

Instantly share code, notes, and snippets.

View ybogdanov's full-sized avatar

Yuriy Bogdanov ybogdanov

View GitHub Profile
@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');
})
@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 / 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 / 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 / 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 / getUserSummary_nodejs_async.js
Created August 20, 2011 13:19
nodejs callbackDriven vs node-async vs node-sync
function getUserSummary(userId, callback)
{
async.parallel({
user : function(callback) {
db.users.findUserById(userId, callback)
},
tweets : function(callback) {
db.users.getTweets(userId, callback)
@ybogdanov
ybogdanov / createEdge-async-example.js
Created August 21, 2011 22:51
examples of code which uses node-async vs node-sync with more complex logic
createEdge : function(e, callback)
{
var self = this, tryNum = 0,
isolateRetryAttempts = 20,
isolateRetryInterval = 1000;
callback = callback || function(){};
if (!(e instanceof Edge) || !e.isValid()) {
return callback(new Error("Invalid edge passed"));
@ybogdanov
ybogdanov / node-sync-express-mysql-transactions.js
Created August 30, 2011 15:23
how to use node-sync in express-middleware style with mysql transactions
// Заюзай node-sync v0.1.9beta2, в ней есть .asyncMiddleware()
// это маленькая магия, которая позволяет писать "синхронные" функции типа function(req, res, next)
// еще не знаю, как ее лучше назвать...
app.post('/q/register', function(req, res) {
var mysql = database.spawnConnection();
// А можно реализацию beginSync?
mysql.beginSync();
@ybogdanov
ybogdanov / erlang_brute.erl
Created October 12, 2011 01:19
brute force example on erlang
-module(brute).
-export([brute/1]).
brute_(N, M, _, O) when N == M ->
O;
brute_(N, M, L, O) ->
[string:right(integer_to_list(N), L, $0) | brute_(N + 1, M, L, O)].
brute(N) ->
brute_(0, math:pow(10, N), N, []).
@ybogdanov
ybogdanov / olx-previews.user.js
Created June 2, 2015 12:31
Makes OLX (slando) render large image previews
// ==UserScript==
// @name OLX Previews
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description Makes OLX (slando) render large image previews
// @match http://kiev.ko.olx.ua/nedvizhimost*
// @copyright 2015+, Yuriy Bogdanov
// ==/UserScript==
;(function(){