简体   繁体   中英

Node.js npm opn - can't get callback function to work

I am trying to get a callback function to work using the opn package (here is the documentation: https://github.com/sindresorhus/opn ).

In short, I'd like to open a specific URL via a browser, wait for the user to close the browser, then run the callback function. When I run my code everything seems to work as expected, however, it doesn't seem to run the callback (I never see 'worked' in the console).

Here is some example code of what I'm trying to do:

var opn = require('opn') opn('http://www.google.com', {app: 'firefox', wait: true}, function(err) { if(err) throw err console.log('worked') })

It does seem to wait (as a note, I'm running this on windows, the module requires an app be explicitly specified in order for it to wait).

I'd like to run code via the callback, after the browser is closed.

I'm pretty new to node, so any insight is greatly appreciated!

The module doesn't seem to have an up-to-date documentation

var opn = require('opn');
opn('http://www.google.com', {
    app: 'Chrome',
    wait: true
}).then(function(cp) {
    console.log('child process:',cp);
    console.log('worked');
}).catch(function(err) {
    console.error(err);
});

The above works, using a thenable promise pattern instead of a callback.

You should report this on the github repository.


UPDATE : an ES6 version:

import opn from 'opn';
opn('http://www.google.com', {
  app: 'Chrome',
  wait: true
}).then(cp => console.log('child process:', cp)).catch(console.error);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM