我看到的代码是这样的:
myObj.doSome("task").then(function(env) {
// logic
});
那么()从何而来?
我看到的代码是这样的:
myObj.doSome("task").then(function(env) {
// logic
});
那么()从何而来?
当前回答
这是关于在箭头函数中使用花括号{}的问题:
这3个例子做的是同样的事情(什么都没有,但是有有效的语法,并且是一个有效的Promise链!)
新承诺(函数(ok) { ok ( /* myFunc1(param1, param2, ..) */ ) })(函数(){ /* myFunc1成功*/ /*启动其他内容*/ /* console.log(whateverparam1) */ /* myFunc2(whateverparam1, otherparam, ..) */ })(函数(){ /* myFunc2 succeed */ /*启动其他内容*/ /* myFunc3(whatever38, ..) */ }) console.log(“这段代码没有错误GG!”)
相同的逻辑使用箭头函数省略{}
new Promise((ok) => ok( /* myFunc1(param1, param2, ..) */ ).then(() => 0 // HEY DID YOU NOTICE! A number that does nothing, // but otherwise the parsing will fail! // The code is pretty clean but have a major downside // As arrow functions without {} can contains only one declaration // console.log("something") will FAIL here ).then(() => "" // HEY DID YOU NOTICE! An empty string that does nothing, // but otherwise the parsing will fail! // As arrow functions without {} can contains only one declaration // We can't add more code here, hence: // console.log("something") // Will break the whole promise // This is likely the error in y(our) code ;) )) console.log("This code has no errors GG!")
带有{}的箭头函数
新承诺((ok) => { ok ( /* myFunc1(param1, param2, ..) */ ) })。那么(()=> { /* myFunc1成功*/ /*启动其他内容*/ })。那么(()=> { /* myFunc2 succeed */ /*启动其他内容*/ /* myFunc3(whatever38, ..) */ console.log(“什么”) /*更多控制台日志!* / console.log(“其他东西”) }) console.log(“这段代码没有错误GG!”)
其他回答
在这种情况下,then()是doSome()方法返回的对象的类方法。
这是关于在箭头函数中使用花括号{}的问题:
这3个例子做的是同样的事情(什么都没有,但是有有效的语法,并且是一个有效的Promise链!)
新承诺(函数(ok) { ok ( /* myFunc1(param1, param2, ..) */ ) })(函数(){ /* myFunc1成功*/ /*启动其他内容*/ /* console.log(whateverparam1) */ /* myFunc2(whateverparam1, otherparam, ..) */ })(函数(){ /* myFunc2 succeed */ /*启动其他内容*/ /* myFunc3(whatever38, ..) */ }) console.log(“这段代码没有错误GG!”)
相同的逻辑使用箭头函数省略{}
new Promise((ok) => ok( /* myFunc1(param1, param2, ..) */ ).then(() => 0 // HEY DID YOU NOTICE! A number that does nothing, // but otherwise the parsing will fail! // The code is pretty clean but have a major downside // As arrow functions without {} can contains only one declaration // console.log("something") will FAIL here ).then(() => "" // HEY DID YOU NOTICE! An empty string that does nothing, // but otherwise the parsing will fail! // As arrow functions without {} can contains only one declaration // We can't add more code here, hence: // console.log("something") // Will break the whole promise // This is likely the error in y(our) code ;) )) console.log("This code has no errors GG!")
带有{}的箭头函数
新承诺((ok) => { ok ( /* myFunc1(param1, param2, ..) */ ) })。那么(()=> { /* myFunc1成功*/ /*启动其他内容*/ })。那么(()=> { /* myFunc2 succeed */ /*启动其他内容*/ /* myFunc3(whatever38, ..) */ console.log(“什么”) /*更多控制台日志!* / console.log(“其他东西”) }) console.log(“这段代码没有错误GG!”)
截至ECMAScript6
.then()方法已经包含在纯JavaScript的Promises中。
来自Mozilla文档:
then()方法返回一个Promise。它有两个参数:callback 有关承诺成功及失败个案的功能。
Promise对象定义为
Promise对象用于延迟和异步 计算。Promise表示未完成的操作 然而,但在未来是值得期待的。
也就是说,Promise充当一个尚未计算,但将来将被解析的值的占位符。然后,.then()函数用于在Promise被解析时关联将要在Promise上调用的函数——无论是解析成功还是解析失败。
ECMAScript6之前
据我所知,在javascript中还没有内置的then()方法(在撰写本文时)。
无论doSome(“task”)返回的是什么,似乎都有一个调用then的方法。
如果您将doSome()的返回结果记录到控制台,您应该能够看到返回结果的属性。
console.log( myObj.doSome("task") ); // Expand the returned object in the
// console to see its properties.
我大概晚了8年,嗯……不管怎样,我真的不知道then()做什么,但也许MDN可能有一个答案。实际上,我可能更懂一点。
这将向您显示所需的所有信息(希望如此)。除非有人已经发布了这个链接。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
格式为promise.prototype.then() promise和prototype有点像变量,但不像javascript中的变量,我的意思是像navigator。getbattery()。then()这样的东西,这个确实存在,但很少在网络上使用,这个显示设备电池的状态,如果你好奇,MDN上还有更多信息。
doSome("task")必须返回一个promise对象,而这个promise总是有一个then函数,所以你的代码就像这样
promise.then(function(env) {
// logic
});
这只是一个普通的成员函数调用。