@@ -309,33 +309,52 @@ export class PythonShell extends EventEmitter {
309309 * Runs a Python script and returns collected messages
310310 * @param {string } scriptPath The path to the script to execute
311311 * @param {Options } options The execution options
312- * @param {Function } callback The callback function to invoke with the script results
313- * @return {Promise } the output from the python script
312+ * @param {Function } (deprecated argument) callback The callback function to invoke with the script results
313+ * @return {Promise<string[]> | PythonShell } the output from the python script
314314 */
315- static run ( scriptPath : string , options ?: Options , callback ?: ( err ?: PythonShellError , output ?: any [ ] ) => any ) {
315+ static run ( scriptPath : string , options ?: Options , callback ?: ( err ?: PythonShellError , output ?: any [ ] ) => any ) {
316+
317+ if ( callback ) {
318+ console . warn ( 'PythonShell.run() callback is deprecated. Use PythonShell.run() promise instead.' )
319+
320+ return this . runLegacy ( scriptPath , options , callback ) ;
321+ }
322+ else {
323+ return new Promise ( ( resolve , reject ) => {
324+ let pyshell = new PythonShell ( scriptPath , options ) ;
325+ let output = [ ] ;
326+
327+ pyshell . on ( 'message' , function ( message ) {
328+ output . push ( message ) ;
329+ } ) . end ( function ( err ) {
330+ if ( err ) reject ( err ) ;
331+ else resolve ( output ) ;
332+ } ) ;
333+ } ) ;
334+ }
335+ } ;
336+
337+ private static runLegacy ( scriptPath : string , options ?: Options , callback ?: ( err ?: PythonShellError , output ?: any [ ] ) => any ) {
316338 let pyshell = new PythonShell ( scriptPath , options ) ;
317339 let output = [ ] ;
318340
319- return new Promise ( ( resolve , reject ) => {
320- return pyshell . on ( 'message' , function ( message ) {
321- output . push ( message ) ;
322- } ) . end ( function ( err ) {
323- if ( err ) reject ( err ) ;
324- else resolve ( output ) ;
325- return callback ( err ? err : null , output . length ? output : null ) ;
326- } ) ;
341+ return pyshell . on ( 'message' , function ( message ) {
342+ output . push ( message ) ;
343+ } ) . end ( function ( err ) {
344+ return callback ( err ? err : null , output . length ? output : null ) ;
327345 } ) ;
328346 } ;
329347
330348
349+
331350 /**
332351 * Runs the inputted string of python code and returns collected messages. DO NOT ALLOW UNTRUSTED USER INPUT HERE!
333352 * @param {string } code The python code to execute
334353 * @param {Options } options The execution options
335354 * @param {Function } callback The callback function to invoke with the script results
336355 * @return {PythonShell } The PythonShell instance
337356 */
338- static runString ( code : string , options ?: Options , callback ?: ( err : PythonShellError , output ?: any [ ] ) => any ) {
357+ static runString ( code : string , options ?: Options , callback ?: ( err : PythonShellError , output ?: any [ ] ) => any ) {
339358
340359 // put code in temp file
341360 const randomInt = getRandomInt ( ) ;
0 commit comments