Skip to content

Write async code in a sync style with generators

License

Notifications You must be signed in to change notification settings

zamirdan/syncme

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

syncme

Write async code in a sync style with js generators.

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);

About

Write async code in a sync style with generators

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published