Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: audit code for new Promise(async) (#8601)

Pavel Feldman26e7c2825b71
diff --git a/src/server/frames.ts b/src/server/frames.ts
index 09cb3aaa14..082caa79f1 100644
--- a/src/server/frames.ts
+++ b/src/server/frames.ts
@@ -928,14 +928,7 @@ export class Frame extends SdkObject {
let result: dom.ElementHandle;
let error: Error | undefined;
let cspMessage: ConsoleMessage | undefined;
- const actionPromise = new Promise<void>(async resolve => {
- try {
- result = await func();
- } catch (e) {
- error = e;
- }
- resolve();
- });
+ const actionPromise = func().then(r => result = r).catch(e => error = e);
const errorPromise = new Promise<void>(resolve => {
listeners.push(eventsHelper.addEventListener(this._page, Page.Events.Console, (message: ConsoleMessage) => {
if (message.type() === 'error' && message.text().includes('Content Security Policy')) {
diff --git a/src/server/trace/recorder/tracing.ts b/src/server/trace/recorder/tracing.ts
index 11febc57e4..efe46b0097 100644
--- a/src/server/trace/recorder/tracing.ts
+++ b/src/server/trace/recorder/tracing.ts
@@ -190,19 +190,18 @@ export class Tracing implements InstrumentationListener, SnapshotterDelegate, Ha
const state = this._recording!;
const zipFile = new yazl.ZipFile();
const failedPromise = new Promise<Artifact>((_, reject) => (zipFile as any as EventEmitter).on('error', reject));
- const succeededPromise = new Promise<Artifact>(async fulfill => {
+ const succeededPromise = new Promise<Artifact>(fulfill => {
zipFile.addFile(state.traceFile, 'trace.trace');
zipFile.addFile(state.networkFile, 'trace.network');
const zipFileName = state.traceFile + '.zip';
for (const sha1 of state.sha1s)
zipFile.addFile(path.join(this._resourcesDir, sha1), path.join('resources', sha1));
zipFile.end();
- await new Promise(f => {
- zipFile.outputStream.pipe(fs.createWriteStream(zipFileName)).on('close', f);
+ zipFile.outputStream.pipe(fs.createWriteStream(zipFileName)).on('close', () => {
+ const artifact = new Artifact(this._context, zipFileName);
+ artifact.reportFinished();
+ fulfill(artifact);
});
- const artifact = new Artifact(this._context, zipFileName);
- artifact.reportFinished();
- fulfill(artifact);
});
return Promise.race([failedPromise, succeededPromise]);
});
diff --git a/src/test/profiler.ts b/src/test/profiler.ts
index f7daa91fca..482da26461 100644
--- a/src/test/profiler.ts
+++ b/src/test/profiler.ts
@@ -38,10 +38,10 @@ export async function stopProfiling(workerIndex: number | undefined) {
if (!profileDir)
return;
- await new Promise<void>(f => session.post('Profiler.stop', async (err, { profile }) => {
+ await new Promise<void>(f => session.post('Profiler.stop', (err, { profile }) => {
if (!err) {
- await fs.promises.mkdir(profileDir, { recursive: true });
- await fs.promises.writeFile(path.join(profileDir, workerIndex === undefined ? 'runner.json' : 'worker' + workerIndex + '.json'), JSON.stringify(profile));
+ fs.mkdirSync(profileDir, { recursive: true });
+ fs.writeFileSync(path.join(profileDir, workerIndex === undefined ? 'runner.json' : 'worker' + workerIndex + '.json'), JSON.stringify(profile));
}
f();
}));