当前位置: 代码迷 >> 综合 >> ES6 —— Promise
  详细解决方案

ES6 —— Promise

热度:74   发布时间:2023-12-21 19:22:05.0

不使用 ES6 Promise
嵌套两个 setTimeout 回调函数

setTimeout(function(){console.log('hello'); // 一秒后输出 “hello”setTimeout(function(){console.log('world'); // 两秒后输出 “world”}, 1000)
}, 1000);

使用 ES6 Promise ,实现以上相同效果

var waitSecond = function() {return new Promise(function(resolve, reject) {setTimeout(resolve, 1000);});
}waitSecond().then(function(){console.log('hello'); // 一秒后输出 “hello”return waitSecond();
}).then(function(){console.log('world'); // 两秒后输出 “world”
});