[英]Are Promises in JavaScript like Exception Handling in Java?
Just like the try(), catch() and throw() in Java as an exception handling is like then() and catch() in JavaScript as a promise!? 就像Java中的try(),catch()和throw()作为异常处理一样,JavaScript中的then()和catch()作为承诺! Just a beginner asking some questions.
只是一个初学者问一些问题。 Please don't judge.
请不要判断。 xD
xD
Not really.. In JavaScript we have try/catch block as well. 在JavaScript中,我们也有try / catch块。
Main benefits of Promises in JavaScript are things like Chaining. JavaScript中Promises的主要好处是诸如Chaining之类的东西。
The equivalent of Promises in Java is CompletableFuture . Java中的Promises等效于CompletableFuture 。 read about :)
阅读 :)
No, they're quite different. 不,它们完全不同。 Exception handling handles exceptions.
异常处理处理异常。 Promises are a way to return values and act on them in an asynchronous environment, which JavaScript is.
承诺是一种在异步环境(JavaScript是)中返回值并对其执行操作的方法。
Suppose you want to customize the JS confirm box. 假设您要自定义JS确认框。 You could set up a jQuery dialog box, and return yes or no, depending on which button the user clicked.
您可以设置一个jQuery对话框,并根据用户单击哪个按钮返回是或否。 Like this:
像这样:
function msgBoxConfirm(msgText, e) {
e.preventDefault(); // Cancel the default behavior
e.stopPropagation(); // Stop any other events from firing down the line
$('#myDialogDiv').html(msgText).dialog({
modal: true,
title: boxTitle,
buttons: {
'Yes': function() {
$(this).dialog('close');
return true;
},
'No': function() {
$(this).dialog('close');
return false;
}
}
});
}
Now, you call your message box function: 现在,您调用消息框函数:
var retval = msgBoxConfirm('Do you really want to do that?', e);
if (!retval) {
//Do the no behavior
} else {
//Do the yes behavior
}
What you will find is that this code will continue before it finds out the value of retval
, so retval
will have a value of undefined
when you evaluate it with the if
. 您会发现,此代码将在找到
retval
的值之前继续执行,因此当您使用if
对其求值时, retval
的值将为undefined
。 This is what it means to be asynchronous; 这就是异步的含义。 the call to your function doesn't wait until the function is done executing before it moves on, potentially causing all sorts of bad behavior.
对函数的调用不会等到函数执行完毕后再继续运行,从而可能导致各种不良行为。
Promises are a way of waiting until the value is returned before checking it. 承诺是一种等待直到返回值之后再检查它的方法。 To see how to set up this example properly using promises, see this .
要了解如何使用Promise正确设置此示例,请参见this 。
try/catch
detect the error itself and transfer control to catch part. try/catch
检测错误本身并转移控制以捕获部分。 this syntax exists in js
too. 这个语法也存在于
js
。
but in then/catch
, you must notify occurring an error yourself (with calling reject
). 但是在
then/catch
,您必须自己通知发生错误(通过调用reject
)。 This means you can notify error
when you recognize an error is occurred. 这意味着您可以在发现
error
时通知error
。 that error can be a real or not real error (programming/logical error). 该错误可以是真实错误,也可以不是真实错误 (编程/逻辑错误)。
Promises and exception handling are different, they are not same. 承诺和异常处理是不同的,它们是不同的。 In javascript promises give us a away to use .
在javascript中,promise让我们可以使用。 then to know when the function has completed its execution , it helps to know when async functions have completed its execution,and javascript also provides try/catch support as you know as that of java.
然后知道函数何时完成执行,有助于了解异步函数何时完成执行,并且javascript还提供了对java的try / catch支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.