1.0.7 • Published 9 years ago

syncme v1.0.7

Weekly downloads
3
License
ISC
Repository
github
Last release
9 years ago

syncme: Write async code in a sync style with js generators.

Git: https://github.com/zamirdan/syncme

How to use:

npm install syncme

Example of use:

var fs = require('fs');

var syncme = require('syncme');

//syncme.sync - use to warp call to async code the

//the yield call will puase the execution, until fs.writeFile finished

//result is object with 3 fields error, result, args {error:error, result:result args:args}

//syncme.sync expects to get as paramters an async func to call the the pramater to send to this function.

//syncme.sync expects that the async func last pramamter is a callcabk node style (err, result)

//any way all the callcabk paramters can be found in result.args... just for case....

//just look the code....

function* syncFile (){

var fileName = 'test.txt';

var textToWrite = 'Hi from syncme';

var result = yield syncme.sync(fs.writeFile,fileName , textToWrite);

var readFileResult = yield syncme.sync(fs.readFile, fileName);

//print file text

console.log(readFileResult.result.toString());

//delete the file

var removeFileResult = yield syncme.sync(fs.unlink, fileName);

if(result.error){ throw new Error (result.error);} }

// run the generator....

syncme.run(syncFile());


You can also cache function using "syncme.createSyncFunction"

Example: var writeFile = syncme.createSyncFunction(fs.writeFile);

Than you can replace:
var FileCreateResult = yield syncme.sync(fs.writeFile,fileName , textToWrite); with: var FileCreateResult = writeFile(fileName , textToWrite);

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago