//当异步代码执行成功时,我们才会调用resolve(...), 当异步代码失败时就会调用reject(...)
//在本例中,我们使用setTimeout(...)来模拟异步代码,
let myFirstPromise = new Promise(function (resolve, reject) {
setTimeout(function () {
Math.random() > 0.5 ? resolve("成功!") : reject("失败!");
}, 500);
});
//success的值是上面调用resolve(...)方法传入的值.
myFirstPromise.then(function (success) {
console.log("success! " + success);
}, function (error) {
console.log("error! " + error);
});