deno.land / x / rambda@v9.1.1 / source / produceAsync.js

produceAsync.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { map } from './map.js'import { type } from './type.js'
function promisify({ condition, input, prop }){ return new Promise((resolve, reject) => { if (type(condition) !== 'Promise'){ return resolve({ type : prop, payload : condition(input), }) }
condition(input) .then(result => { resolve({ type : prop, payload : result, }) }) .catch(err => reject(err)) })}
function produceFn(conditions, input){ let asyncConditionsFlag = false for (const prop in conditions){ if ( asyncConditionsFlag === false && type(conditions[ prop ]) === 'Promise' ){ asyncConditionsFlag = true } }
if (asyncConditionsFlag === false){ const willReturn = {} for (const prop in conditions){ willReturn[ prop ] = conditions[ prop ](input) }
return Promise.resolve(willReturn) }
const promised = [] for (const prop in conditions){ const condition = conditions[ prop ] promised.push(promisify({ input, condition, prop, })) }
return new Promise((resolve, reject) => { Promise.all(promised) .then(results => { const willReturn = {}
map(result => willReturn[ result.type ] = result.payload, results)
resolve(willReturn) }) .catch(err => reject(err)) })}
export function produceAsync(conditions, input){ if (arguments.length === 1){ return async _input => produceFn(conditions, _input) }
return new Promise((resolve, reject) => { produceFn(conditions, input).then(resolve) .catch(reject) })}
rambda

Version Info

Tagged at
2 months ago