promise多个then,如何不使用catch跳出中间的then
发布于 5 年前 作者 helloHT 3729 次浏览 来自 问答

function fun(callback){ Promise.then() .then(() => {}) .then(() => {}) .then(() => {callback()}) .then(() => {}) .then(() => {}) } 一个回调函数包含着一个promise,我想执行到callback就结束,而不执行后面的then,请问有什么优雅的写法么?

3 回复

function fun(callback){ Promise.then() .then(() => {}) .then(() => {}) .then(() => { callback(); return Promise.reject(); }) .then(() => {}) .then(() => {}) }

这样可以么?

这样,也许是个方案

const P = Promise.resolve(1)
const M = Promise.resolve(1)

async function fun(callback){
  
  await P.then()
  return callback
  await M.then()
  
  return 111
}

回到顶部