-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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