promise的了解

promise的了解

  • 依照 Promise/A+ 的定义,Promise 有四种状态:
    • pending: 初始状态, 非 fulfilledrejected.
    • fulfilled: 成功的操作.
    • rejected: 失败的操作.
    • settled: Promise已被fulfilledrejected,且不是pending
  • 另外, fulfilledrejected一起合称 settled
  • Promise 对象用来进行延迟(deferred) 和异步(asynchronous) 计算

Promise 的构造函数

  • 构造一个 Promise,最基本的用法如下:
var promise = new Promise(function(resolve, reject) {

        if (...) {  // succeed

            resolve(result);

        } else {   // fails

            reject(Error(errMessage));

        }
    });
  • Promise 实例拥有 then 方法(具有 then 方法的对象,通常被称为thenable)。它的使用方法如下:
promise.then(onFulfilled, onRejected)
  • 接收两个函数作为参数,一个在 fulfilled 的时候被调用,一个在rejected的时候被调用,接收参数就是 futureonFulfilled 对应resolve, onRejected对应 reject

个人博客地址:大家可以看看

发布了20 篇原创文章 · 获赞 20 · 访问量 528

猜你喜欢

转载自blog.csdn.net/qq_41108972/article/details/104588868