promises 很酷,但很多人并没有理解就在用了
高级错误#1:不了解Promise.resolve()
new Promise(function (resolve, reject) {
resolve(11);
}).then(function(data){console.log(data)});
var aa = function(){
console.log(111)
return 1;
}
Promise.resolve(aa).then(function(data){console.log(data)})
Promise.reject(new Error('some awful error'));
高级错误#3:promises vs promise工厂
function executeSequentially(promiseFactories) {
var result = Promise.resolve();
promiseFactories.forEach(function (promiseFactory) {
result = result.then(promiseFactory);
});
return result;
}
高级错误#5:promises丢失
Promise.resolve('foo').then(function () {
return Promise.resolve('bar');
}).then(function (result) {
console.log(result);
});