huoxiangdong / Blog

学习记录
0 stars 0 forks source link

异步 #26

Open huoxiangdong opened 6 years ago

huoxiangdong commented 6 years ago
huoxiangdong commented 6 years ago

EventEmitter

huoxiangdong commented 6 years ago

Promise执行顺序

let promise = new Promise(function(resolve, reject) {
  console.log('Promise');
  resolve();
});

promise.then(function() {
  console.log('resolved.');
});

console.log('Hi!');

// Promise
// Hi!
// resolved

Promise 新建后立即执行,所以首先输出的是Promise。 然后,then方法指定的回调函数,将在当前脚本所有同步任务执行完才会执行,所以resolved最后输出

huoxiangdong commented 6 years ago

await在等啥

var t = async function() { 
       return 1
    }
var s = async function () {
         console.log(t())  // 返回结果: Promise {<resolved>: 1}
         console.log(await t())  // 返回结果:1
    }
s()