source: node_modules/ramda-adjunct/es/async.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1import { curryN, bind } from 'ramda';
2import resolveP from './resolveP';
3import rejectP from './rejectP';
4
5/**
6 * Takes a generator function and returns an async function.
7 * The async function returned is a curried function whose arity matches that of the generator function.
8 *
9 * Note: This function is handy for environments that does support generators but doesn't support async/await.
10 *
11 * @func async
12 * @memberOf RA
13 * @since {@link https://char0n.github.io/ramda-adjunct/2.16.0|v2.16.0}
14 * @category Function
15 * @sig Promise c => (a, b, ...) -> a -> b -> ... -> c
16 * @param {Function} generatorFn The generator function
17 * @return {Function} Curried async function
18 * @see {@link https://www.promisejs.org/generators/}
19 * @example
20 *
21 * const asyncFn = RA.async(function* generator(val1, val2) {
22 * const a = yield Promise.resolve(val1);
23 * const b = yield Promise.resolve(val2);
24 *
25 * return a + b;
26 * });
27 *
28 * asyncFn(1, 2); //=> Promise(3)
29 *
30 */
31var async = curryN(1, function (generatorFn) {
32 function asyncWrapper() {
33 var iterator = bind(generatorFn, this).apply(void 0, arguments);
34 var handle = function handle(result) {
35 var resolved = resolveP(result.value);
36 return result.done ? resolved : resolved.then(function (value) {
37 return handle(iterator.next(value));
38 }, function (error) {
39 return handle(iterator["throw"](error));
40 });
41 };
42 try {
43 return handle(iterator.next());
44 } catch (error) {
45 return rejectP(error);
46 }
47 }
48 if (generatorFn.length > 0) {
49 return curryN(generatorFn.length, asyncWrapper);
50 }
51 return asyncWrapper;
52});
53export default async;
Note: See TracBrowser for help on using the repository browser.