首页 > 文章列表 > js中可选的回调函数

js中可选的回调函数

js 回调函数
266 2022-08-06

1、then的回调函数不是严格要求必写的。如果不写,promise也会用上一个返回的最终值来传递。

doSomething().then().then(function(result) {
  console.log('got a result', result);
});
 
// 输出结果是:
//
// got a result 42

2、如果当前的then没有传输回调函数,该函数将直接使用前一个promise返回的最终值来解决下一个promise。

if(!handler.onResolved) {
  handler.resolve(value);
  return;
}