JavaScript函数调用返回问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liqianglai/article/details/53156245

三种方式,返回同样的结果

let f1 = (): Promise=> {
  return new Promise((resolve, reject)=> {
    resolve("f1");
  });
}

let f2 = (str): Promise=> {
  return new Promise((resolve, reject)=> {
    resolve("f2" + str);
  });
}

console.log("方式一");
f1()
  .then(result=> {
    return f2(result)
  })
  .then(result => {
    return console.log(result)
  });

console.log("方式二");
f1()
  .then(result=> f2(result))
  .then(result => console.log(result));

console.log("方式三");
f1()
  .then(f2)
  .then(console.log);

结论

方式一
方式一
方式一
f2f1
f2f1
f2f1

console.log首先执行是因为Promise的原因

猜你喜欢

转载自blog.csdn.net/liqianglai/article/details/53156245
今日推荐