如何在匿名函数中应用 node.js REPL?
How to apply node.js REPL in an anonymous function?
不幸的是,我对 node.js 的了解为零,因为直到现在我都使用 Ruby 及其名为 Pry 的 REPL。我发现 node.js 也有这样的包,可以通过 "npm" 包管理器安装。我这样做的原因是 node.js 包 "facebook-chat-api" 它对于以编程方式发送 facebook 聊天消息很有用,据我所知这在 Ruby 中无法实现(或者也许在其他语言也一样)。我安装了在此处 https://www.npmjs.com/package/facebook-chat-api 找到的软件包并尝试成功,帮助示例(face.js 我有 运行 它与 "node face.js"):
var login = require("facebook-chat-api");
login({email: "XXX.XXX@XXX.XX", password: "XXXXXX"}, function(err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
});
在为用户设置正确的 ID 后,它成功地发送了消息,没有任何缺陷。然后我也安装了一个 REPL,叫做 "locus" (https://www.npmjs.com/package/locus),因为我想在消息发送后停止 node.js 脚本,并从 REPL 命令行发送另一个.所以我的脚本变成了以下内容:
var login = require("facebook-chat-api");
var locus = require('locus')
login({email: "XXX.XXX@XXX.XX", password: "XXXXXX"}, function(err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
eval(locus);
});
不幸的是,我的第二个脚本没有按预期运行。我确实收到 "locus" REPL 提示,但直到我使用命令 "quit" 退出 REPL 后,Facebook 聊天消息才会发送。我想在消息发送后恰好停止我的脚本,我想获得 REPL 提示,然后如果可能的话再次从 REPL 调用 "api.sendMessage"。我能做什么或如何重组我的脚本以使其按预期工作。也许将匿名函数放入真正的命名函数中,但我不知道如何正确地做到这一点。
我做了一个小测试,使用 setTimeout 进行异步请求,并在您仍在现场时伪造发送请求。
这是代码:
var locus = require('locus');
function login () {
setTimeout(function () {
console.log('message sent');
},2000);
}
login();
eval(locus);
这是我在其中输入一些命令的控制台。
——————————————————————————————————————————————————————————————————————————
3 : function login () {
4 : setTimeout(function () {
5 : console.log('message sent');
6 : },2000);
7 : }
8 :
9 : login();
10 :
ʆ: message sent // 2 seconds after the repl opened the first message sent
typeof login
'function' // locus is aware of the login function
ʆ: login();
login(); // run the login function
undefined
ʆ: message sent // the message was (fake) sent without quitting
login(); // test a second send
undefined
ʆ: message sent // another message was sent.
如果以上代码显示了您预期的行为,您的代码可能是:
var login = require("facebook-chat-api");
var locus = require('locus');
login({email: "XXX.XXX@XXX.XX", password: "XXXXXX"}, loginHandler);
eval(locus);
function loginHandler (err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
}
不幸的是,我对 node.js 的了解为零,因为直到现在我都使用 Ruby 及其名为 Pry 的 REPL。我发现 node.js 也有这样的包,可以通过 "npm" 包管理器安装。我这样做的原因是 node.js 包 "facebook-chat-api" 它对于以编程方式发送 facebook 聊天消息很有用,据我所知这在 Ruby 中无法实现(或者也许在其他语言也一样)。我安装了在此处 https://www.npmjs.com/package/facebook-chat-api 找到的软件包并尝试成功,帮助示例(face.js 我有 运行 它与 "node face.js"):
var login = require("facebook-chat-api");
login({email: "XXX.XXX@XXX.XX", password: "XXXXXX"}, function(err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
});
在为用户设置正确的 ID 后,它成功地发送了消息,没有任何缺陷。然后我也安装了一个 REPL,叫做 "locus" (https://www.npmjs.com/package/locus),因为我想在消息发送后停止 node.js 脚本,并从 REPL 命令行发送另一个.所以我的脚本变成了以下内容:
var login = require("facebook-chat-api");
var locus = require('locus')
login({email: "XXX.XXX@XXX.XX", password: "XXXXXX"}, function(err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
eval(locus);
});
不幸的是,我的第二个脚本没有按预期运行。我确实收到 "locus" REPL 提示,但直到我使用命令 "quit" 退出 REPL 后,Facebook 聊天消息才会发送。我想在消息发送后恰好停止我的脚本,我想获得 REPL 提示,然后如果可能的话再次从 REPL 调用 "api.sendMessage"。我能做什么或如何重组我的脚本以使其按预期工作。也许将匿名函数放入真正的命名函数中,但我不知道如何正确地做到这一点。
我做了一个小测试,使用 setTimeout 进行异步请求,并在您仍在现场时伪造发送请求。
这是代码:
var locus = require('locus');
function login () {
setTimeout(function () {
console.log('message sent');
},2000);
}
login();
eval(locus);
这是我在其中输入一些命令的控制台。
——————————————————————————————————————————————————————————————————————————
3 : function login () {
4 : setTimeout(function () {
5 : console.log('message sent');
6 : },2000);
7 : }
8 :
9 : login();
10 :
ʆ: message sent // 2 seconds after the repl opened the first message sent
typeof login
'function' // locus is aware of the login function
ʆ: login();
login(); // run the login function
undefined
ʆ: message sent // the message was (fake) sent without quitting
login(); // test a second send
undefined
ʆ: message sent // another message was sent.
如果以上代码显示了您预期的行为,您的代码可能是:
var login = require("facebook-chat-api");
var locus = require('locus');
login({email: "XXX.XXX@XXX.XX", password: "XXXXXX"}, loginHandler);
eval(locus);
function loginHandler (err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
}