// , callback 1 function someAsyncFunction(a, b, callback) { setTimeout(function(){ callback(null, a + b); }, 1000) } // , Function.prototype.sync(), // , call() // "" , var result = someAsyncFunction.sync(null, 2, 3); console.log(result); // "5" 1
var fs = require('fs'), Sync = require('sync'); // Sync(function(){ // --> // , Function.prototype.sync() var source = fs.readFile.sync(null, __filename); // console.log(String(source)); })
// - , // , function asyncFunction(callback) { var p_client = new Db('test', new Server("127.0.0.1", 27017, {})); p_client.open(function(err, p_client) { if (err) return callback(err); // <-- p_client.createCollection('test_custom_key', function(err, collection) { if (err) return callback(err); // <-- collection.insert({'a':1}, function(err, docs) { if (err) return callback(err); // <-- collection.find({'_id':new ObjectID("aaaaaaaaaaaa")}, function(err, cursor) { if (err) return callback(err); // <-- cursor.toArray(function(err, items) { if (err) return callback(err); // <-- // = items callback(null, items); }); }); }); }); }); }
function syncFunction(callback) { // Sync(function(){ var p_client = new Db('test', new Server("127.0.0.1", 27017, {})); p_client.open.sync(p_client); var collection = p_client.createCollection.sync(p_client, 'test'); collection.insert.sync(collection, {'a' : 1}); var cursor = collection.find.sync(collection, {'_id':new ObjectID("aaaaaaaaaaaa")) var items = cursor.toArray.sync(cursor); // = items return items; }, callback) // <-- callback }
var syncFunction = function() { var p_client = new Db('test', new Server("127.0.0.1", 27017, {})); p_client.open.sync(p_client); var collection = p_client.createCollection.sync(p_client, 'test'); collection.insert.sync(collection, {'a' : 1}); var cursor = collection.find.sync(collection, {'_id':new ObjectID("aaaaaaaaaaaa")) var items = cursor.toArray.sync(cursor); // = items return items; }.async() // <--
var Sync = require('sync'); // - , function someAsyncFunction(a, b, callback) { setTimeout(function(){ callback(null, a + b); }, 1000) } // Sync(function(){ // // , var results = Sync.Parallel(function(callback){ someAsyncFunction(2, 2, callback()); someAsyncFunction(5, 5, callback()); }); console.log(results); // [ 4, 10 ] // var results = Sync.Parallel(function(callback){ someAsyncFunction(2, 2, callback('foo')); // assign the result to 'foo' someAsyncFunction(5, 5, callback('bar')); // assign the result to 'bar' }); console.log(results); // { foo: 4, bar: 10 } })
// Sync(function(){ // someAsyncFunction, var foo = someAsyncFunction.future(null, 2, 2); var bar = someAsyncFunction.future(null, 5, 5); // foo, bar - console.log(foo); // { [Function: Future] result: [Getter], error: [Getter] } // , foo bar console.log(foo.result, bar.result); // 4 10 - ( ) })
$ npm install sync
$ node-fibers my_file.js
var Sync = require('sync'); // , fn - -, Sync(fn) // , fn - -, / callback Sync(fn, callback) // callback() Sync.Parallel(function(callback){ callback() // ( ) callback('foo') // }) // / // obj - , Function.prototype.sync(obj, arg1, arg2) // , // / Future, getter 'result' // Future.result, , // obj - , Function.prototype.future(obj, arg1, arg2) // - // , // obj - Function.prototype.async(obj)
Source: https://habr.com/ru/post/116124/
All Articles