3 回答
TA贡献1869条经验 获得超4个赞
promiseA的then函数返回一个新的承诺(promiseB)被立即解决后promiseA解决了,它的价值是什么,从内成功函数返回的值promiseA。
在这种情况下promiseA,使用值result解析- 然后立即promiseB使用值解析result + 1。
访问值的promiseB方式与访问结果的方式相同promiseA。
promiseB.then(function(result) {
// here you can use the result of promiseB});TA贡献1874条经验 获得超12个赞
当一个promise被解决/拒绝时,它会调用它的成功/错误处理程序:
var promiseB = promiseA.then(function(result) {
// do something with result});该then方法还返回一个promise:promiseB,它将根据来自promiseA的成功/错误处理程序的返回值进行解析/拒绝。
promiseA的成功/错误处理程序可以返回三个可能的值,这将影响promiseB的结果:
1. Return nothing --> PromiseB is resolved immediately, and undefined is passed to the success handler of promiseB2. Return a value --> PromiseB is resolved immediately, and the value is passed to the success handler of promiseB3. Return a promise --> When resolved, promiseB will be resolved. When rejected, promiseB will be rejected. The value passed to the promiseB's then handler will be the result of the promise
有了这种理解,您可以理解以下内容:
promiseB = promiseA.then(function(result) {
return result + 1;});then调用立即返回promiseB。当promiseA解决后,它会将结果传递给promiseA的成功处理程序。由于返回值是promiseA的结果+ 1,因此成功处理程序返回一个值(上面的选项2),因此promiseB将立即解析,并且promiseB的成功处理程序将传递promiseA的结果+ 1。
TA贡献1830条经验 获得超3个赞
.thenpromiseB的函数接收.thenpromiseA函数返回的内容。
这里promiseA返回的是一个数字,它将作为numberpromiseB成功函数中的参数提供。然后将增加1
添加回答
举报
