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

This commit is contained in:
Pavel Feldman 2021-08-31 15:55:32 -07:00 committed by GitHub
parent 1c169289b2
commit 26e7c2825b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 17 deletions

View File

@ -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')) {

View File

@ -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]);
});

View File

@ -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();
}));