Skip to content

每日一题 - 以下四个promise有什么不同

信息卡片

  • 时间:2019-07-25
  • tag:ES6 Promise

题目描述

js
doSomething().then(function () {
  return doSomethingElse();
});

doSomething().then(function () {
  doSomethingElse();
});

doSomething().then(doSomethingElse());

doSomething().then(doSomethingElse);
doSomething().then(function () {
  return doSomethingElse();
});

doSomething().then(function () {
  doSomethingElse();
});

doSomething().then(doSomethingElse());

doSomething().then(doSomethingElse);

考察点

Promise的then方法

参考答案

  1. then方法提供一个自定义的回调函数,若传入非函数,则会忽略当前then方法。
  2. 在回调函数中会把上一个then中返回的值当做参数值供当前then方法调用。
  3. then方法执行完毕后需要返回一个新的值给下一个then调用(没有返回值默认使用undefined)。
  4. 每个then只可能使用前一个then的返回值