GithubHelp home page GithubHelp logo

meteor-async's Introduction

Meteor Async

Set of async utilities to work with NPM modules and other async code blocks.

Installation

meteor add meteorhacks:async

API

Available in the Server Side only

Meteor APIs are executed synchronously. Most of the NodeJS modules works asynchronously. So we need a way to bride the gap. Async Utilities comes to rescue you.

Async.runSync(function)

Async.runSync() pause the execution until you invoke done() callback as shown below.

var response = Async.runSync(function(done) {
  setTimeout(function() { 
    done(null, 1001);
  }, 100);
});

console.log(response.result); // 1001

done() callback takes 2 arguments. error and the result object. You can get them as the return value of the Async.runSync() as shown as response in the above example.

return value is an object and it has 2 fields. error and result.

Meteor.sync(function)

Same as Async.runSync but deprecated.

Async.wrap(function)

Wrap an asynchronous function and allow it to be run inside Meteor without callbacks.

//declare a simple async function
function delayedMessge(delay, message, callback) {
  setTimeout(function() {
    callback(null, message);
  }, delay);
}

//wrapping
var wrappedDelayedMessage = Async.wrap(delayedMessge);

//usage
Meteor.methods({
  'delayedEcho': function(message) {
    var response = wrappedDelayedMessage(500, message);
    return response;
  }
});

If the callback has a result, it will be returned from the wrapped function. If there is an error, it will be thrown.

Async.wrap(function) is very similar to Meteor._wrapAsync.

Async.wrap(object, functionName)

Very similar to Async.wrap(function), but this API can be used to wrap an instance method of an object.

var github = new GithubApi({
    version: "3.0.0"
});

//wrapping github.user.getFrom
var wrappedGetFrom = Async.wrap(github.user, 'getFrom');

Async.wrap(object, functionNameList)

Very similar to Async.wrap(object, functionName), but this API can be used to wrap multiple instance methods of an object.

GithubApi = Npm.require('github');

var github = new GithubApi({
    version: "3.0.0"
});

//wrapping github.user.getFrom and github.user.getEmails
var wrappedGithubUser = Async.wrap(github.user, ['getFrom', 'getEmails']);

//usage
var profile = wrappedGithubUser.getFrom({user: 'arunoda'});
var emails = wrappedGithubUser.getEmails();

meteor-async's People

Contributors

arunoda avatar orangewise avatar

Stargazers

Chris Hearn avatar Иван Вольнов avatar Grégory Horion avatar Carlos Alvidrez avatar Paul Ray avatar ANAND S avatar Marian Klühspies avatar yangchenglong avatar Mikhail Proniushkin avatar Dead J. Dona avatar  avatar Wai Yan Yoon avatar Alec Troemel avatar Andreas Røyrvik avatar magic-wind avatar  avatar JVercout avatar forgng avatar GladiusK avatar Will avatar Nicolas Azari avatar Martin Schultheiß avatar Yoshi Fujimoto avatar Max avatar  avatar wanli avatar Satya van Heummen avatar Lyn avatar Kima avatar  avatar Frazer avatar Ross Martin avatar  avatar THIAO-LAYEL Bruno avatar John Kenn avatar Sung avatar Michael Anthony avatar Michael Nino Evensen avatar Vu Tran avatar Ryan Deussing avatar Fabio Dias Rollo avatar Charleno Pires avatar  avatar Lance Johnson avatar Chet Corcos avatar Mark avatar crapthings avatar Mykola Striletskyi avatar Hongjun Wen avatar Ben Wong avatar Michael Shilman avatar Sergey Tolmachev avatar Tarcisio Gruppi avatar  avatar  avatar B.E. Henriksen avatar alan blount avatar Henry J avatar Jidé avatar Pierre-Eric Marchandet avatar  avatar Philipp Muens avatar Bozhao avatar

Watchers

James Cloos avatar Carlos Alvidrez avatar Michael Anthony avatar Vu Tran avatar  avatar

meteor-async's Issues

timeout on wrap method?

What if a wrapped method never calls the callback, wouldn't the "event loop" for that fiber get stuck?

wouldn't be a good idea to have a timeout and throw an exception or something around these lines?

Calling Async.wrap stops function processing

I call this function in the startup of my app. I expect it to run and loop. The Async task I would assume would execute and then continue on but once the asyncGameUpdater runs, it stops the processing.

function gameUpdateTask(){
    var asyncGameUpdater = Async.wrap(gameUpdater);
    asyncGameUpdater();
    var i=0;
    while (i < config.openInAdvance){
        syncGameUpdater();
        i++;

    }
}

Async.runSync() returns error

When I use Async.runSync(...), I get the following error on the client:

details: undefined
error: 500
errorType: "Meteor.Error"
message: "Internal server error [500]"
reason: "Internal server error"

When I switch back to Meteor.sync(...), everything works perfectly again, even though Meteor.sync() is supposed to be deprecated. Can I continue to use Meteor.sync()?

Wrapped calls within a runSync block do not resolve

If I have a function func being called in a runSync block atop an item which is only retrieved asynchronously

Async.runSync((done) => {
  asyncProcess((err, thing) => done(err, func(thing)));
});

then it will succeed so long as func is itself a direct, synchronous call. In actuality, func may be a function derived from a call to Async.wrap

function func(thing) {
  return Async.wrap(thing.goGetAnotherThing)();
}

However, in this circumstance it appears that the wrapped function does not resolve properly as in my (convoluted, unsharable as of yet) testing the original call will resolve as null.

It's very possible that there's merely a bug in my code somewhere, but this appears to be some kind of synchronicity/fibers/futures interaction difficulty. Any suggestions?

Is there any way to use this when the callback isn't the last argument?

I've read through the README, and this discovermeteor post, and the wrapAsync docs in Meteor core.

It works fine, when the "next" argument is the callback...

But take a look at this NPM method I'm trying to wrap:
https://github.com/moshen/node-googlemaps/blob/master/lib/googlemaps.js#L153

when I only pass in the origin and destination arguments, it works fine... when I do any others, it fails because the wrapAsync doesn't know where to pass in the callback.

Suggestions?

Reference error on Async

I don't understand why, but after running meteor install meteorhacks:async, I am unable to use Async.<anything>. I get a ReferenceError. I've spent hours trying to find some reason why, but I've found nothing. I had already installed meteorhacks:npm, and I thought async was already part of it, since they are documented together. But I got the ReferenceError before installing async and still get it after. Any idea what could be wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.