Skip to content

Commit 81e18b4

Browse files
robrichardlilianammmatos
authored andcommitted
simplify promise dispatch logic
1 parent 4b71558 commit 81e18b4

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/execution/dispatcher.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ export class Dispatcher {
1515
this._patches = [];
1616
}
1717

18-
execute(fn: () => PromiseOrValue<mixed>, errors: Array<GraphQLError>) {
18+
execute(
19+
fn: () => PromiseOrValue<mixed>,
20+
errors: Array<GraphQLError>,
21+
): Promise<mixed> {
1922
try {
20-
return fn();
23+
const data = fn();
24+
if (isPromise(data)) {
25+
return data.then(undefined, error => {
26+
errors.push(error);
27+
return Promise.resolve(null);
28+
});
29+
}
30+
return Promise.resolve(data);
2131
} catch (error) {
2232
errors.push(error);
23-
return null;
33+
return Promise.resolve(null);
2434
}
2535
}
2636

@@ -31,28 +41,15 @@ export class Dispatcher {
3141
errors: Array<GraphQLError>,
3242
) {
3343
this._patches.push(
34-
Promise.resolve(this.execute(fn, errors)).then(data => {
35-
if (isPromise(data)) {
36-
return data.then(undefined, error => ({
37-
value: {
38-
data: null,
39-
path: pathToArray(path),
40-
label,
41-
errors: [error],
42-
},
43-
done: false,
44-
}));
45-
}
46-
return {
47-
value: {
48-
data,
49-
path: pathToArray(path),
50-
label,
51-
...(errors && errors.length > 0 ? { errors } : {}),
52-
},
53-
done: false,
54-
};
55-
}),
44+
this.execute(fn, errors).then(data => ({
45+
value: {
46+
data,
47+
path: pathToArray(path),
48+
label,
49+
...(errors && errors.length > 0 ? { errors } : {}),
50+
},
51+
done: false,
52+
})),
5653
);
5754
}
5855

0 commit comments

Comments
 (0)