Javascript承诺:当涉及Javascript远程调用时,如何链接结果?
这更多的是一个正确语法的问题。下面的代码在远程JavaScript调用完成之前以启动序列生成控制台日志输出。我对承诺完全是个新手,所以我肯定我的语法错了 以下是两段相关代码的示例:Javascript承诺:当涉及Javascript远程调用时,如何链接结果?,javascript,promise,Javascript,Promise,这更多的是一个正确语法的问题。下面的代码在远程JavaScript调用完成之前以启动序列生成控制台日志输出。我对承诺完全是个新手,所以我肯定我的语法错了 以下是两段相关代码的示例: var getNode = function(sfid,override) { return new Promise(function(resolve,reject){ var text = ""; VisJSController.returnObjectsJson(s
var getNode = function(sfid,override) {
return new Promise(function(resolve,reject){
var text = "";
VisJSController.returnObjectsJson(sfid,
function(result, event) {
if(event.type === 'exception') {
console.log("exception");
console.log(event);
} else if (event.status) {
result.Nodes.forEach(function(entry)
{
addNode(entry.id,entry.label,entry.shape,eval(entry.image));
if (entry.id != sfid && !(edgeIds.includes(entry.id+sfid) || edgeIds.includes(sfid+entry.id)))
{
addEdge(sfid,entry.id);
console.log('Adding Edge');
console.log(sfid);
console.log(entry.id);
}
else
{
console.log('Edge detected');
console.log(entry.id);
console.log(sfid);
console.log(edgeIds);
}
});
resolve(true);
} else {
console.log(event.message);
}
}
);
});
}
function startup()
{
getNode('{!SF_ID}',true).then(console.log('finished'));
}
在本例中,我希望在getNode完成远程请求处理后,在末尾显示完成的日志。
then(console.log('finished')代码>。然后
需要一个函数作为参数。您正在提供调用函数的结果<代码>然后(()=>console.log('finished')
可以做你想做的事(没有在代码主体中查找任何问题,但是问题很明显)另一个问题是,承诺解决的唯一时间是}else if(event.status){
…在其他情况下,承诺仍然没有得到解决-您可能希望在其他条件下拒绝?感谢@JaromandaX添加语法以传递结果(即使我没有使用它)生成所需的结果。当我将其更改为getNode({!SF_ID}),true。然后(函数(result){console.log('finished')),一切都很好。