new Promise(function (resolve, reject) { });
//
var success = true;
var myPromise = new Promise(function (resolve, reject) {
if (success) {
resolve("异步操作成功了");
} else {
reject("有意外错误了");
}
});
console.log(myPromise);
//
var myPromise = Promise.reject("");
myPromise.then(function(vaule){
console.log("执行",value);
});
var promise = new Promise(function (resolve, reject) {
var a = 1
if (a === 1) {
resolve(a)
} else {
reject(error)
}
});
promise.then(function (value) {
console.log(value++)
return value
}).catch(function (error) {
console.log(error)
}).then(function (value) {
console.log(value++)
})
// 输出:
// 1
// 2
let mylog = (msg)=>console.log(msg);
mylog("日志1");
var p = new Promise(function (resolve, reject) {
mylog("日志2");
});
p.then(mylog("日志3"));
//Promise.all
console.log("Begin Promise.all 演示");
Promise.all([
new Promise(function(resolve){
setTimeout(function(){
resolve(100);
},100);
}),
new Promise(function(resolve){
setTimeout(function(){
resolve(200);
},200);
}),
new Promise(function(resolve){
setTimeout(function(){
resolve(300);
},300);
})
]).then(function(value){
console.log(value); //[100,200,300]
});
console.log("End Promise.all 演示");
//Promise.race
console.log("Begin Promise.race 演示");
Promise.race([
new Promise(function(resolve){
setTimeout(function(){
resolve(100);
},100);
}),
new Promise(function(resolve){
setTimeout(function(){
resolve(200);
},200);
}),
new Promise(function(resolve){
setTimeout(function(){
resolve(300);
},300);
})
]).then(function(value){
console.log(value); //100
});
console.log("End Promise.race 演示");
console