当服务器使用 MeteorJS 完成一个功能时向客户端发送一些东西
Send something to the client when the server has finish a function with MeteorJS
我正在尝试向客户端发送一个布尔值以了解服务器的异步调用何时完成。为此,我与其他一些函数做了一个 Promise,如下所示:
'container.stop': function(id) {
return new Promise(Meteor.bindEnvironment(function(resolve) {
var ctn = docker.getContainer(id);
ctn.stop(Meteor.bindEnvironment(function(err, data) {
if (err) {
resolve(false);
} else {
console.log(data);
resolve(true);
}
}));
}));
},
所以这是一个有效的 Promise 示例,客户端在完成时收到
但我现在使用的功能有点不同,我找不到如何 return 向客户发送某些内容:
'machine.stop': function(name) {
new Machine(name).stop(Meteor.bindEnvironment(function(err) {
console.log("Server (stop):" + name)
if (!err) {
checkAlerte(name);
InfosMachines.upsert({
nameMachine: name,
}, {
nameMachine: name,
stateMachine: 'stopped'
});
upsertCollectionMachines();
//inspect the machine we have created to get her name + data + id
new Machine(name).inspect(Meteor.bindEnvironment(function(err, result) {
if (!err) {
// get all datasources
var myDatasource;
var theIDToDelete;
return new Promise(Meteor.bindEnvironment(function(resolve) {
// take the ds with the same name as the host
HTTP.call("GET", 'http://localhost:3000/api/datasources/name/' + ("datasource_" + result.name), {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': APIKEY,
},
},
function(error, result) {
if (!error) {
myDatasource = result.data;
resolve(myDatasource);
} else {
console.error(error);
}
});
})).then(Meteor.bindEnvironment(function(myDatasource) {
// delete the ds with the id
deleteDataSource(myDatasource.id);
resolve(true);
}));
}
}));
} else {
console.error(err);
}
}));
},
这里客户端除了服务器的代码工作之外什么也没有收到(机器停止并且调用了 deleteDatasource
正如你所看到的,当 deleteDatasource
函数被调用时,我想 return 一些东西。
有人可以帮助我吗?
我承诺等待 "myDataSource" 执行 then(
的方式是否正确?
[编辑] 当最后一部分完成后,我是否应该对 return 做这样的事情:
then(Meteor.bindEnvironment(function(myDatasource /*cest la valeur de myDatasource*/) {
console.log("4TH step");
// delete the ds with the id
deleteDataSource(myDatasource.id);
return new Promise(Meteor.bindEnvironment(function(resolve) {
resolve(true);
}));
}));
如果 Meteor 方法 return 是 Promise,它会在 return 发送给客户端之前解决。
您的 container.stop
方法 return 是一个承诺,这就是为什么您传递给 resolve
的结果被 return 发送给客户端的原因。 machine.stop
没有 return 值,因此 Meteor 服务器在回答 Meteor.call.
之前不会等待结果
在@JaromandaX 和@aedm 的帮助下,我发现我应该放置另一个包含所有代码的 Promise:
'machine.stop': function(name) {
return new Promise(Meteor.bindEnvironment(function(resolve) {
new Machine(name).stop(Meteor.bindEnvironment(function(err, data) {
console.log("Server (stop):" + name)
console.log("1ST STEP");
if (!err) {
checkAlerte(name);
InfosMachines.upsert({
nameMachine: name,
}, {
nameMachine: name,
stateMachine: 'stopped'
});
upsertCollectionMachines();
//inspect the machine we have created to get her name + data + id
new Machine(name).inspect(Meteor.bindEnvironment(function(err, result) {
console.log("2ND STEP");
if (!err) {
// get all datasources
var myDatasource;
var theIDToDelete;
new Promise(Meteor.bindEnvironment(function(resolve) {
console.log("3RD step");
// take the ds with the same name as the host
HTTP.call("GET", 'http://localhost:3000/api/datasources/name/' + ("datasource_" + result.name), {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': APIKEY,
},
},
function(error, result) {
if (!error) {
myDatasource = result.data;
//dit a then que myDataSource est fait
resolve(myDatasource);
} else {
console.error(error);
}
});
})).then(Meteor.bindEnvironment(function(myDatasource /*cest la valeur de myDatasource*/) {
console.log("4TH step");
// delete the ds with the id
deleteDataSource(myDatasource.id);
}));
resolve(true);
}
}));
} else {
console.error(err)
}
}));
}));
},
我正在尝试向客户端发送一个布尔值以了解服务器的异步调用何时完成。为此,我与其他一些函数做了一个 Promise,如下所示:
'container.stop': function(id) {
return new Promise(Meteor.bindEnvironment(function(resolve) {
var ctn = docker.getContainer(id);
ctn.stop(Meteor.bindEnvironment(function(err, data) {
if (err) {
resolve(false);
} else {
console.log(data);
resolve(true);
}
}));
}));
},
所以这是一个有效的 Promise 示例,客户端在完成时收到
但我现在使用的功能有点不同,我找不到如何 return 向客户发送某些内容:
'machine.stop': function(name) {
new Machine(name).stop(Meteor.bindEnvironment(function(err) {
console.log("Server (stop):" + name)
if (!err) {
checkAlerte(name);
InfosMachines.upsert({
nameMachine: name,
}, {
nameMachine: name,
stateMachine: 'stopped'
});
upsertCollectionMachines();
//inspect the machine we have created to get her name + data + id
new Machine(name).inspect(Meteor.bindEnvironment(function(err, result) {
if (!err) {
// get all datasources
var myDatasource;
var theIDToDelete;
return new Promise(Meteor.bindEnvironment(function(resolve) {
// take the ds with the same name as the host
HTTP.call("GET", 'http://localhost:3000/api/datasources/name/' + ("datasource_" + result.name), {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': APIKEY,
},
},
function(error, result) {
if (!error) {
myDatasource = result.data;
resolve(myDatasource);
} else {
console.error(error);
}
});
})).then(Meteor.bindEnvironment(function(myDatasource) {
// delete the ds with the id
deleteDataSource(myDatasource.id);
resolve(true);
}));
}
}));
} else {
console.error(err);
}
}));
},
这里客户端除了服务器的代码工作之外什么也没有收到(机器停止并且调用了 deleteDatasource
正如你所看到的,当 deleteDatasource
函数被调用时,我想 return 一些东西。
有人可以帮助我吗?
我承诺等待 "myDataSource" 执行 then(
的方式是否正确?
[编辑] 当最后一部分完成后,我是否应该对 return 做这样的事情:
then(Meteor.bindEnvironment(function(myDatasource /*cest la valeur de myDatasource*/) {
console.log("4TH step");
// delete the ds with the id
deleteDataSource(myDatasource.id);
return new Promise(Meteor.bindEnvironment(function(resolve) {
resolve(true);
}));
}));
如果 Meteor 方法 return 是 Promise,它会在 return 发送给客户端之前解决。
您的 container.stop
方法 return 是一个承诺,这就是为什么您传递给 resolve
的结果被 return 发送给客户端的原因。 machine.stop
没有 return 值,因此 Meteor 服务器在回答 Meteor.call.
在@JaromandaX 和@aedm 的帮助下,我发现我应该放置另一个包含所有代码的 Promise:
'machine.stop': function(name) {
return new Promise(Meteor.bindEnvironment(function(resolve) {
new Machine(name).stop(Meteor.bindEnvironment(function(err, data) {
console.log("Server (stop):" + name)
console.log("1ST STEP");
if (!err) {
checkAlerte(name);
InfosMachines.upsert({
nameMachine: name,
}, {
nameMachine: name,
stateMachine: 'stopped'
});
upsertCollectionMachines();
//inspect the machine we have created to get her name + data + id
new Machine(name).inspect(Meteor.bindEnvironment(function(err, result) {
console.log("2ND STEP");
if (!err) {
// get all datasources
var myDatasource;
var theIDToDelete;
new Promise(Meteor.bindEnvironment(function(resolve) {
console.log("3RD step");
// take the ds with the same name as the host
HTTP.call("GET", 'http://localhost:3000/api/datasources/name/' + ("datasource_" + result.name), {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': APIKEY,
},
},
function(error, result) {
if (!error) {
myDatasource = result.data;
//dit a then que myDataSource est fait
resolve(myDatasource);
} else {
console.error(error);
}
});
})).then(Meteor.bindEnvironment(function(myDatasource /*cest la valeur de myDatasource*/) {
console.log("4TH step");
// delete the ds with the id
deleteDataSource(myDatasource.id);
}));
resolve(true);
}
}));
} else {
console.error(err)
}
}));
}));
},