I have a bunch of code in express which needs to be run synchronously. To achieve this I am using fibers/future
library. The code is working properly but I am not confident that if it is the best way to achieve sync code from fibers/future
.
CODE
Functions
var api_object = Future.wrap(function(arg1, callback){ var obj = new API(arg1.config.consumer_key, arg1.config.consumer_secret); return callback(null, obj); });
var create_customer = Future.wrap(function(api_object, name, callback){
api_object.create_customer_app(
{"name": name
}, function(err,api_respone){
return callback(null, api_response);
}); });
- API
router.route('/create_customer').post(function(req, res){
var arg1 = req.body.arg;
var customer_name = req.body.customer_name;
Future.task(function(){
var api_object = func.api_obejct(arg1).wait();
var customer_create = func.create_customer(api_object, name).wait(); }).detach(); });
Here as you can see I am using fibers/future
to make my async calls work in sync manner. I am using .wait()
to wait for the command to complete and binding the tasks and functions in future. Is this approach correct?