SOURCE

var timeout = function (ms) {
    return new Promise((reslove, reject) => {
        setTimeout(reslove, ms, "done");
    });
}

timeout(100).then(value => {
    console.log(value);
})


//包装Ajax请求
const getJSON = function (url) {
    const promise = new Promise(function (resolve, reject) {
        const handler = function () {
            if (this.readyState !== 4) {
                return;
            }
            if (this.status === 200) {
                resolve(this.response);
            } else {
                reject(new Error(this.statusText));
            }
        };
        const client = new XMLHttpRequest();
        client.open("GET", url);
        client.onreadystatechange = handler;
        client.responseType = "json";
        client.setRequestHeader("Accept", "application/json");
        client.send();

    });

    return promise;
};

getJSON("http://localhost:6346/CarCascade/GetVehicleAttributes?attributeIds=0-0").then(function (json) {
    console.log('Contents: ' + json);
}, function (error) {
    console.error('出错了', error);
});

//Promise.race用法
{
    var p1 = new Promise((reslove, reject) => {
        $.ajax({
            url:'http://localhost:6346/CarCascade/GetVehicleAttributes?attributeIds=0-0',
            type:'get',
            success:function(data){
                reslove(data);
            },
            error:function(xhr,e){
                console.log(e);
                reject(e);
            }
        });
    });
    var p2 = new Promise((relove, reject) => {
        setTimeout(() => {
            reject('超时了')
        }, 5000);
    });
    var dataPromise = Promise.race([p1, p2])
        .then(data=>console.log(data))
        .catch(err=>console.log(err));

}
console 命令行工具 X clear

                    
>
console