@@ -16,6 +16,7 @@ import {
1616 handleLogEvent ,
1717 newCompilePathRequest ,
1818 newCompileStringRequest ,
19+ promiseWithResolvers ,
1920} from './utils' ;
2021import { compilerCommand } from '../compiler-path' ;
2122import { activeDeprecationOptions } from '../deprecations' ;
@@ -128,23 +129,29 @@ export class AsyncCompiler {
128129 ) ;
129130 dispatcher . logEvents$ . subscribe ( event => handleLogEvent ( options , event ) ) ;
130131
131- const compilation = new Promise < proto . OutboundMessage_CompileResponse > (
132- ( resolve , reject ) =>
133- dispatcher . sendCompileRequest ( request , ( err , response ) => {
134- this . compilations . delete ( compilation ) ;
135- // Reset the compilation ID when the compiler goes idle (no active
136- // compilations) to avoid overflowing it.
137- // https://github.com/sass/embedded-host-node/pull/261#discussion_r1429266794
138- if ( this . compilations . size === 0 ) this . compilationId = 1 ;
139- if ( err ) {
140- reject ( err ) ;
141- } else {
142- resolve ( response ! ) ;
143- }
144- } ) ,
145- ) ;
132+ // Avoid `new Promise()` here because `dispatcher.sendCompilerequest` can
133+ // run its callback synchronously, so `compilation` needs to be assigned
134+ // and in `this.compilations` before we call it.
135+ const {
136+ promise : compilation ,
137+ resolve,
138+ reject,
139+ } = promiseWithResolvers < proto . OutboundMessage_CompileResponse > ( ) ;
146140 this . compilations . add ( compilation ) ;
147141
142+ dispatcher . sendCompileRequest ( request , ( err , response ) => {
143+ this . compilations . delete ( compilation ) ;
144+ // Reset the compilation ID when the compiler goes idle (no active
145+ // compilations) to avoid overflowing it.
146+ // https://github.com/sass/embedded-host-node/pull/261#discussion_r1429266794
147+ if ( this . compilations . size === 0 ) this . compilationId = 1 ;
148+ if ( err ) {
149+ reject ( err ) ;
150+ } else {
151+ resolve ( response ! ) ;
152+ }
153+ } ) ;
154+
148155 return handleCompileResponse ( await compilation ) ;
149156 } finally {
150157 activeDeprecationOptions . delete ( optionsKey ) ;
0 commit comments