sync-please
v1.0.0
Published
Make asynchronous functions synchronous
Downloads
2
Readme
sync-please
sync-please is a tiny node package that uses fibers to let you call an asynchronous function synchronously. It attempts to minimize the difference between calling a function synchronously and asynchronously, and unlike many other packages, it supports synchronizing nested asynchronous calls.
Installation
Make sure node.js and npm are installed and use npm to install the package: npm install sync-please
.
Usage
fibers = require "fibers"
makeSynchronous = require "sync-please"
# You need to wrap the code you want to run synchronously in a fiber.
(fibers ->
module =
asynchronousFunction: (param1, param2, callback) ->
setTimeout callback, 2000
# Asynchronous call.
module.asynchronousFunction, "param1", "param2", ->
console.log "Callback 1"
console.log "Done 1"
# Synchronous call.
makeSynchronous module, module.asynchronousFunction, "param1", "param2", ->
console.log "Callback 2"
# Nested synchronous call.
makeSynchronous module, module.asynchronousFunction, "param1", "param2", ->
console.log "Callback 3"
console.log "Done 2"
).run()
This will output:
Done 1
Callback 1
Callback 2
Callback 3
Done 2
Running tests
There is a small Mocha test suite that you should run if you'd like to contribute to sync-please. Make sure you have Mocha installed and just run mocha
from the root directory.
Changelog
1.0.0
- Initial release.
License
Copyright (c) 2012 Jarod Long
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.