Skip to content

Commit

Permalink
chore(async): upgrade async.js
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres committed Oct 12, 2016
1 parent 83aa4f1 commit 018dc2b
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions js/deps/async.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*jshint -W082 */
/*globals define*/
/*globals define, self*/
'use strict';
{
// async function takes a generator, and optional "this"
Expand All @@ -10,31 +10,21 @@
// returns returns a function asyncFunction that returns promise
// It is called with zero or more arguments...
return function asyncFunction(...args) {
return new Promise((resolve, reject) => {
let gen;
if (func.constructor.name === 'GeneratorFunction') {
gen = func.call(self, ...args);
} else { // Wrap it
gen = (function*() {
return func.call(self, ...args);
}());
}

step(gen.next());

function step({value, done}) {
if (done) {
return resolve(value);
}
Promise
.resolve(value) // Normalize thenable
.then(
result => step(gen.next(result)),
error => step(gen.throw(error))
)
.catch(reject);
}
});
const gen = (function*() {
return (func.constructor.name === 'GeneratorFunction') ?
yield* func.call(self, ...args) : func.call(self, ...args);
}());
try {
return step(gen.next());
} catch (err) {
return Promise.reject(err);
}
function step({value, done}) {
const p = Promise.resolve(value); // Normalize thenables
return (done) ? p : p
.then(result => step(gen.next(result)))
.catch(error => step(gen.throw(error)));
}
};
}

Expand Down

0 comments on commit 018dc2b

Please sign in to comment.