Notes

JavaScript Promise 构造器 API

const aPromise = new Promise(function(resolve, reject) {
  // some successful circumstances
  resolve(aValue);
    
  // some failed circumstances
  reject(err);
});

构建非 pending 的 Promise

创建一个已被拒绝的 Promise, 有两种方法

const rejectedPromiseA = new Promise(function(resolve, reject) {
  reject(err);
});
    
const rejectedPromiseB = Promise.reject(err);

创建一个已完成的 Promise

const resolvedTh = {
  then: function(callback) {
    // do something
  },
};
    
const rejectedTh = {
  then: function(err, callback) {
    // do something with err or callback
  },
};
    
const promiseA = Promise.resolve(resovledTh);
const promiseB = Promise.resolve(rejectedTh);