Skip to content

Commit

Permalink
feat(async): add initial implementation
Browse files Browse the repository at this point in the history
Closes #830
  • Loading branch information
char0n committed Feb 14, 2019
1 parent 51a64ee commit 9ee6434
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { curryN, bind } from 'ramda';

import resolveP from './resolveP';
import rejectP from './rejectP';

const async = generatorFn =>
curryN(generatorFn.length, function asyncWrapper(...args) {
const iterator = bind(generatorFn, this)(...args);

const handle = result => {
const resolved = resolveP(result.value);

return result.done
? resolved
: resolved.then(
value => handle(iterator.next(value)),
error => handle(iterator.throw(error))
);
};

try {
return handle(iterator.next());
} catch (error) {
return rejectP(error);
}
});

export default async;

0 comments on commit 9ee6434

Please sign in to comment.