Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用本机javascript实现延迟模式?_Javascript_Es6 Promise - Fatal编程技术网

如何使用本机javascript实现延迟模式?

如何使用本机javascript实现延迟模式?,javascript,es6-promise,Javascript,Es6 Promise,我正在从事一个不使用jquery的项目。所有受支持的浏览器都具有本机承诺 我想复制jquery通过$.deferred提供的延迟模式 //Example var deferred1 = new $.Deferred(); var deferred2 = new $.Deferred(); $.get(someUrl, function(){ ... deferred1.resolve() }) $.get(someUrl, function(){ ... deferred2.

我正在从事一个不使用jquery的项目。所有受支持的浏览器都具有本机承诺

我想复制jquery通过$.deferred提供的延迟模式

//Example
var deferred1 = new $.Deferred();
var deferred2 = new $.Deferred();

$.get(someUrl, function(){
  ...
  deferred1.resolve()
})

$.get(someUrl, function(){
  ...
  deferred2.resolve()
})

$.when(deferred1, deferred2).then(function(){//do stuff})
如何使用本机承诺做到这一点?

请尝试以下方法:

function get(url) {
    //Make and return new promise, it takes a callback: 
    //A function that will be passed other functions via the arguments resolve and reject
    return new Promise((resolve, reject) => {
        var request = new XMLHttpRequest();
        request.addEventListener("load", () => {
            //Success ! we need to resolve the promise by calling resolve.
            resolve(request.responseText);
        });
        request.addEventListener("error", () => {
            //Error! we need to reject the promise by calling reject .
            reject(request.statusCode);
        });
        //Perform the request
        request.open('GET', url);
        request.send();
    });
};

var urls = [
        'https://httpbin.org/ip',
        'https://httpbin.org/user-agent'
];
//Create an array of promises
// is equivalent to 
//var promises = []; for(var i in urls) promises.push(get(url[i]));
var promises  = urls.map(get);

//The Promise.all(iterable) method returns a promise that resolves when 
//all of the promises in the iterable argument have resolved, 
//or rejects with the reason of the first passed promise that rejects.
Promise.all(promises).then(function (responses) {
     console.log(responses);
});
演示:

请尝试以下操作:

function get(url) {
    //Make and return new promise, it takes a callback: 
    //A function that will be passed other functions via the arguments resolve and reject
    return new Promise((resolve, reject) => {
        var request = new XMLHttpRequest();
        request.addEventListener("load", () => {
            //Success ! we need to resolve the promise by calling resolve.
            resolve(request.responseText);
        });
        request.addEventListener("error", () => {
            //Error! we need to reject the promise by calling reject .
            reject(request.statusCode);
        });
        //Perform the request
        request.open('GET', url);
        request.send();
    });
};

var urls = [
        'https://httpbin.org/ip',
        'https://httpbin.org/user-agent'
];
//Create an array of promises
// is equivalent to 
//var promises = []; for(var i in urls) promises.push(get(url[i]));
var promises  = urls.map(get);

//The Promise.all(iterable) method returns a promise that resolves when 
//all of the promises in the iterable argument have resolved, 
//or rejects with the reason of the first passed promise that rejects.
Promise.all(promises).then(function (responses) {
     console.log(responses);
});

演示:

这些不同的是承诺。用同样的方法使用它们。但似乎你需要创建你自己的get来使用承诺——那些不同的就是承诺。用同样的方法使用它们。但似乎你需要创建自己的get来使用承诺-