Skip to content

Instantly share code, notes, and snippets.

@helmus

helmus/async.js Secret

Last active August 29, 2015 14:04
Show Gist options
  • Save helmus/7646b0c470ed7adee1af to your computer and use it in GitHub Desktop.
Save helmus/7646b0c470ed7adee1af to your computer and use it in GitHub Desktop.
The idea behind this concept is to more closely resemble ECMA 7 async functions by annotating functions. If your implementation returns a promise or uses a generator as a coroutine, it's an async function and this tries to make that more clear.
'use strict';
var Promise = require('bluebird');
var generatorSample = function *()
{
};
function isGenerator(func) {
return func instanceof generatorSample.constructor;
}
/**
* ECMA 7 async annotation
* @param func
* @returns {*}
*/
function async(func) {
if (isGenerator(func)) {
return Promise.coroutine(func);
} else {
return Promise.method(func);
}
}
'use strict';
var async = require('async');
var somethingAsync = async(function *(){
yield fooAsync();
yield barAsync();
});
var somethingElseAsync = async(function (){
if (someCondition()) {
return barAsync();
} else {
return null;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment