CNode

promise多个then,如何不使用catch跳出中间的then

问答
HhelloHT发布于7 年前最后回复7 年前3 回复4491 浏览0 收藏

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

查看回复

回复 (3)

L
liujw123#1·7 年前

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

这样可以么?

I
ilovedesert001#2·7 年前

可以

I
ilovedesert001#3·7 年前

这样,也许是个方案

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

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

参与回复
登录后即可参与回复。登录