javascript - 这段代码如何改写为promise形式的?
PHP中文网
PHP中文网 2017-04-11 09:05:18
[JavaScript讨论组]
var count = 0;

setTimeout(function(){
  console.log("step1", count);
  count += 1;
  setTimeout(function(){
    console.log("step2", count);
    count += 1;
    setTimeout(function(){
      console.log("step3", count);
      count += 1;
      setTimeout(function(){
        console.log("step4", count);
        count += 1;
        setTimeout(function(){
          console.log("step5", count);
          count += 1;
          setTimeout(function(){
            console.log("step6", count);
            count += 1;
          }, 1000);
        }, 1000);
      }, 1000);
    }, 1000);
  }, 1000);
}, 1000);
PHP中文网
PHP中文网

认证0级讲师

全部回复(2)
阿神

这么写?

'use strict';

var timer = function(cb, ms) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            cb();
            resolve();
        }, ms);
    });
};

var count = 0;

timer(function() {
    console.log('step1', count);
    count += 1;
}, 1000)
.then(function() {
    return timer(function() {
        console.log('step2', count);
        count += 1;
    }, 1000);
})
.then(function() {
    return timer(function() {
        console.log('step3', count);
        count += 1;
    }, 1000);
})
.then(function() {
    return timer(function() {
        console.log('step4', count);
        count += 1;
    }, 1000);
})
.then(function() {
    return timer(function() {
        console.log('step5', count);
        count += 1;
    }, 1000);
})
.then(function() {
    return timer(function() {
        console.log('step6', count);
        count += 1;
    }, 1000);
});
PHP中文网

如果涉及到变量复用的话用 Promise 可能还稍微麻烦一点,这时候用 async/await 绝对是上乘之举了,在线运行:http://goo.gl/fb29D3

step_by_step就是你要的 Promise 版,后面是配合 async/await 的使用方法。

function step_by_step(count) {
  return new Promise(function(resolve) {
    setTimeout(function(){
      count += 1;
      resolve(count);
    }, 1000);
  });
}

(async function main() {
  let count = 0;
  for(let i=0;i<6;i++) {
    count = await step_by_step(count);
    console.log(`step${count}`, count);
  }
})();

当然直接用 Promise 去写也是可以的,使用外部变量存储后即可,然而两相对比之下,怎么看都还是 async/await 写起来更加优雅呢。

var count = 0;
step_by_step(count).then(function(count){
    console.log(count);
    return step_by_step(count);
}).then(function(count){
    console.log(count);
    return step_by_step(count);
}).then(function(count){
    console.log(count);
    return step_by_step(count);
}).then(function(count){
    console.log(count);
    return step_by_step(count);
}).then(function(count){
    console.log(count);
    return step_by_step(count);
}).then(function(count){
    console.log(count);
    return step_by_step(count);
}).then(function(count){
    console.log(count);
    return step_by_step(count);
});

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号