Skip to content

Commit 36318ff

Browse files
Jacob Parkerben-eb
Jacob Parker
authored andcommitted
Add async support
1 parent ac8372a commit 36318ff

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/__tests__/parser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,15 @@ test('empty selector string', (t) => {
5858
}).process('').result;
5959
});
6060
});
61+
62+
test('async parser', (t) => {
63+
t.plan(1);
64+
65+
t.notThrows(() => {
66+
return parser(() => new Promise((res) => {
67+
res();
68+
})).process('').result.then(() => {
69+
t.pass();
70+
});
71+
});
72+
});

src/processor.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Parser from './parser';
33
export default class Processor {
44
constructor (func) {
55
this.func = func || function noop () {};
6+
this.funcRes = null;
67
return this;
78
}
89

@@ -15,11 +16,17 @@ export default class Processor {
1516
options: options,
1617
});
1718
this.res = input;
18-
this.func(input);
19+
this.funcRes = this.func(input);
1920
return this;
2021
}
2122

2223
get result () {
24+
let isPromise = this.funcRes &&
25+
typeof this.funcRes.then === 'function';
26+
27+
if (isPromise) {
28+
return this.funcRes.then(() => String(this.res));
29+
}
2330
return String(this.res);
2431
}
2532
}

0 commit comments

Comments
 (0)