zoukankan      html  css  js  c++  java
  • promise笔记

     // promise 加载一个图片示例
    function loadImage(url){
      new Promise(function(resolve, reject){
        var img = new Image()
        img.onload = function(){
          resolve(img)
        }
        img.onerror = function(){
          reject(new Error('could not load' + url))
        }
        img.src = url 
      })
    }
    // promise 实现ajax请求
    var getJSON= function(url){
      var promise = new Promise(function(resolve, reject){
        var client = new XMLHttpRequest();
        client.open('GET', url)
        client.onreadystatechange = handler
        client.responseType = 'json'
        client.setRequestHeader('Accept', 'application/json')
        client.send()

        function handler(){
          if(this.readyState !== 4){
            return
          }
          if(this.status === 200){
            resolve(this.response)
          }else{
            reject(new Error(this.statusText))
          }
        }
      })
  • 相关阅读:
    java web 开发 IDE 下载地址
    【转】简述TCP的三次握手过程
    【转】TCP、UDP数据包大小的限制
    复习笔记2018.8.3
    .NET和UNITY版本问题
    LUA全总结
    C++全总结
    C# 全总结
    #region 常量和静态变量静态类readonly
    //todo 的用处
  • 原文地址:https://www.cnblogs.com/victory820/p/7226212.html
Copyright © 2011-2022 走看看