Skip to content

Commit 6d8be00

Browse files
authored
Merge pull request #1195 from webstech/resolve
git api: resolve promise in lineHandler
2 parents b2602e6 + 49b15b8 commit 6d8be00

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

lib/git.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,10 @@ export function git(args: string[], options?: IGitOptions | undefined):
4545
if (!process.stdout) {
4646
throw new Error(`No stdout for "git ${args.join(" ")}`);
4747
}
48-
let linePromise: Promise<void> | undefined;
48+
const promises: Array<Promise<void>> = [];
4949
const handleLine = (line: string): boolean => {
5050
try {
51-
if (!linePromise) {
52-
linePromise = lineHandler(line);
53-
} else {
54-
linePromise = linePromise.then(() => {
55-
return lineHandler(line);
56-
});
57-
}
58-
linePromise.catch((reason) => {
59-
reject(reason);
60-
process.kill();
61-
});
51+
promises.push(lineHandler(line));
6252
} catch (reason) {
6353
reject(reason);
6454
process.kill();
@@ -84,12 +74,7 @@ export function git(args: string[], options?: IGitOptions | undefined):
8474
if (buffer.length > 0) {
8575
handleLine(buffer);
8676
}
87-
if (linePromise) {
88-
linePromise.then(() => { resolve(""); })
89-
.catch((reason) => { reject(reason); });
90-
} else {
91-
resolve("");
92-
}
77+
Promise.all(promises).then(() => resolve("")).catch(reject);
9378
});
9479
};
9580
}
@@ -107,8 +92,10 @@ export function git(args: string[], options?: IGitOptions | undefined):
10792
}':\nstderr: ${result.stderr
10893
}\nstdout: ${result.stdout}\n`);
10994
}
110-
resolve(!options || options.trimTrailingNewline === false ?
111-
result.stdout : trimTrailingNewline(result.stdout));
95+
if (!options?.lineHandler) { // let callback resolve the promise
96+
resolve(!options || options.trimTrailingNewline === false ?
97+
result.stdout : trimTrailingNewline(result.stdout));
98+
}
11299
}).catch((reason) => {
113100
reject(reason);
114101
});

0 commit comments

Comments
 (0)