猿问

promise如何实现异步递归

async(data).then(function (data) {

  if (data < 0) return data;

  return async1(data);

}).then(function (data) {

  if (data < 0) return data;  

  return async1(data);

}).then(function (data) {

  return data

})

如上面的代码,当data < 0时就返回data的值,但是什么时候返回0我不清楚,也就是return async1(data);执行多少次我不清楚,不使用async/await 前提下怎么利用递归实现


哈士奇WWW
浏览 1145回答 3
3回答

冉冉说

如上面的代码,当data < 0时就返回data的值异步没有&nbsp;return&nbsp;, 只有&nbsp;callback&nbsp;.

慕无忌1623718

在chrome控制台下运行(async ()=>{&nbsp; &nbsp; // 模拟每次接受请求都会被动更新状态&nbsp; &nbsp; let counter = 10;&nbsp; &nbsp; let async = async ()=>new Promise((resolve, reject)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(counter--);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, Math.random()*500);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; // 模拟主动更新自身状态&nbsp; &nbsp; let counter2 = 10;&nbsp; &nbsp; let timer = setInterval(()=>{&nbsp; &nbsp; &nbsp; &nbsp; counter2-=1;&nbsp; &nbsp; &nbsp; &nbsp; if(counter2 < 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearInterval(timer);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, 1000);&nbsp; &nbsp; let async2 = async()=>new Promise((resolve, reject)=>{&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(counter2);&nbsp; &nbsp; &nbsp; &nbsp; }, 100); // 假装这里的操作需要点时间&nbsp; &nbsp; });&nbsp; &nbsp; function getSomeData() {&nbsp; &nbsp; &nbsp; return new Promise(async function body(resolve, reject) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; async().then(data=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`async() == ${data}`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data < 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body(resolve, reject);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 50); // 避免过于频繁的调用导致UI卡死&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; function getSomeData2() {&nbsp; &nbsp; &nbsp; return new Promise(async function body(resolve, reject) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; async2().then(data=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`async2() == ${data}`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data < 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body(resolve, reject);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 10); // 避免过于频繁的调用导致UI卡死&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; console.log(`Done with async: ${await getSomeData()}`);&nbsp; &nbsp; console.log(`Done with async2: ${await getSomeData2()}`);})();
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答