我在看fibers/future。我想知道Function.prototype.future
在这种情况下的目的是什么。我尝试了几个实验。
let Future = require('fibers/future');
let socket = require('net').connect(process.env.connection);
waitForMessage = function (message) {
let future = new Future();
let listen = function (fut, data) {
let match = new RegExp(`^${message}$`);
if (match.test(data.toString('ascii'))) {
fut.return();
}
}
socket.addListener('data', listen.bind(this, future));
future.resolve(future, () => {
socket.removeListener('data', listen.bind(this, future));
});
return future;
}
waitForOkay = function (msg) {
waitForMessage('OK').wait();
}.future();
我对如何使用它有一个模糊的理解......但它实际上做了什么?什么时候应该使用?