Node.js npm opn-can't使回调函数工作
Node.js npm opn - can't get callback function to work
我正在尝试使用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);
相关文章:
- JavaScript回调函数
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- 如何正确地将参数传递给RequireJS回调函数
- 回调函数在python代码中离线
- 从AJAX回调函数中分离数据
- 赋值后的回调函数
- 如何在javascript回调函数中返回多个变量
- 函数表达式,返回回调函数
- 等待回调函数执行
- JavaScript回调函数和Google Feed API
- 如何从ajax成功回调函数中读取javascript变量
- 正在保存JavaScript内部回调函数中的值
- 有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)
- 如何在javascript中添加带有回调函数的按钮点击事件作为window.conf
- 在Golang回调函数中启用CORS
- 在初始函数完成之前调用回调函数
- jQuery Mobile:如何在$.Mobile.changePage之前运行回调函数
- 即使使用回调函数也无法返回值
- 为什么prototypjs观察到回调函数有绑定