Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function pget(url) {
        return new Promise(function (resolve, reject) {
            var req = new XMLHttpRequest();
            req.open('GET', url);
            req.onload = function () {
                if(req.status==200){
                    resolve(req.response);
                }
                else {
                    reject(Error(req.statusText));
                }
            };
            req.onerror = function () {
                reject(Error("Network Error"));
            };
            req.send();
        });
    }
    function pgetJSON(url) {
        return pget(url).then(JSON.parse);
    }
function getJokeCategories(){
    var jokesToGet = [];
    var result = {};
    Promise.all(
        [pgetJSON("http://api.icndb.com/categories").then(function(data){
            result.cats = data.value;
        }),
        pgetJSON("http://api.icndb.com/jokes/count").then(function(data){
            for(var i=15; i<20; i++){
                jokesToGet.push("http://api.icndb.com/jokes/"+i);
            }
            result.jokes = jokesToGet;
            Promise.all(
                jokesToGet.map(function(jk){
                    pgetJSON(jk).then(function(joke){
                        console.log(joke, " just returned");
                    });
                })
            ).then(function(){
                console.log("All jokes returned. This should actually happen only when all jokes are retireved.");
            });
        })
        ]
    ).then(function(){
        console.log(result, "This should happen at the very end when joke count, joke categories and all jokes are returned.");
    });
}
getJokeCategories();
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers