var waitSecond = new Promise(function(resolve, reject) { setTimeout(resolve, 1000); }); waitSecond.then(function() { console.log("Hello"); // 1秒後輸出"Hello" return waitSecond; }).then(function() { console.log("Hi"); // 2秒後輸出"Hi" });
new Promise(function(resolve , reject) { resolve(1); }).then(function(val) { console.log(val); return new Promise(function(resolve , reject) { resolve(2); }); }).then(function(val) { console.log(val); return new Promise(function(resolve , reject) { resolve(3); }); }).then(function(val) { console.log(val); return new Promise(function(resolve , reject) { resolve(4); }); }).then(function(val) { console.log(val); }); //輸出: // 1 // 2 // 3 // 4
setTimeout(function () { console.log('three'); }, 0); Promise.resolve().then(function () { console.log('two'); }); console.log('one');