Node.js npm opn-can't使回调函数工作

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

本文关键字:回调 函数 工作 js npm opn-can Node      更新时间:2024-01-14

我正在尝试使用opn包让回调函数工作(以下是文档:https://github.com/sindresorhus/opn)。

简而言之,我想通过浏览器打开一个特定的URL,等待用户关闭浏览器,然后运行回调函数。当我运行代码时,一切似乎都像预期的那样工作,然而,它似乎没有运行回调(我从未在控制台中看到"工作")。

下面是我尝试做的一些示例代码:

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

它似乎在等待(需要注意的是,我在windows上运行这个模块,该模块需要显式指定一个应用程序才能等待)。

我想在浏览器关闭后通过回调运行代码。

我对node还很陌生,所以任何见解都将不胜感激!

模块似乎没有最新的文档

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

上面的工作原理是使用一个可调用的promise模式而不是回调。

您应该在github存储库中报告这一情况。


更新:ES6版本:

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