我正在尝试向客户端发送布尔值,以了解服务器的异步调用何时完成。要做到这一点,我做了一个承诺,其他一些功能如下:
'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示例,客户端在完成后会收到
但我现在使用的功能稍有不同,我找不到如何将某些内容返回给客户端:
'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
函数时返回一些内容。
有人可以帮助我吗?
我做Promise等待“myDataSource”执行then(
的方式是否正确?
[编辑]我应该做这样的事情,在最后一部分完成时返回一些东西:
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);
}));
}));
答案 0 :(得分:0)
如果Meteor方法返回Promise,则在返回客户端之前已解决。
您的container.stop
方法会返回一个承诺,这就是您传递给resolve
的结果返回给客户端的原因。 machine.stop
没有返回值,因此Meteor服务器在回答Meteor.call之前不会等待结果。
答案 1 :(得分:0)
在@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)
}
}));
}));
},