<commit_before>#!/usr/bin/env node

// -----------------------
// BASIC THEN AND CATCH
// -----------------------

function MyPromiseFunction(param1,param2) {
    return new Promise((resolve,reject) => {

        var thingsAreOK = (param1 == 1);

        // If we hit an error, call reject to bail out, pronto.
        if (!thingsAreOK) { reject(new Error("Bailing out!  Not cool to use input param1 = "+param1+".")) }

        // Do slow async shit.
        // Decide when to call resolve() to trigger that we are finished.
        setTimeout(() => resolve("We finished successfully, with input param1 = "+param1), 300);

    })
}
 
// then and catch are async functions, but will only fire once promise is complete.
var myGoodCall = MyPromiseFunction(1,2)
.then(function(result) {
    console.log(result)
})
.catch(function(result) {
    console.log(result)
})

// Same as above, except we pass a "bad" parameter, and we use => shorthand
var myBadCall = MyPromiseFunction(2,2)
.then(result => {
    console.log(result)
})
.catch(result => {
    console.log("We caught an error: "+result)
})

// -----------------------


// -----------------------
// CHAINING
// -----------------------

// function MyPromiseChain

// -----------------------<commit_msg>Wow nice Promise-based RSS updater easy peasy<commit_after>#!/usr/bin/env node


// https://javascript.info/promise-chaining
// "As a rule, an asynchronous action should always return a promise."


// -----------------------
// BASIC THEN AND CATCH
// -----------------------

function MyPromiseFunction(param1,param2) {
    return new Promise((resolve,reject) => {

        var thingsAreOK = (param1 == 1);

        // If we hit an error, call reject to bail out, pronto.
        if (!thingsAreOK) { reject(new Error("Bailing out!  Not cool to use input param1 = "+param1+".")) }

        // Do slow async shit.
        // Decide when to call resolve() to trigger that we are finished.
        setTimeout(() => resolve("We finished successfully, with input param1 = "+param1), 300);

    })
}
 
// then and catch are async functions, but will only fire once promise is complete.
var myGoodCall = MyPromiseFunction(1,2)
.then(function(result) {
    console.log(result)
})
.catch(function(result) {
    console.log(result)
})

// Same as above, except we pass a "bad" parameter, and we use => shorthand
var myBadCall = MyPromiseFunction(2,2)
.then(result => {
    console.log(result)
})
.catch(result => {
    console.log("We caught an error: "+result)
})

// -----------------------


// -----------------------
// ALL
// -----------------------
// TODO
/*
let urls = [
    'https://api.github.com/users/iliakan',
    'https://api.github.com/users/remy',
    'https://api.github.com/users/jeresig'
];
  
Promise.all(urls.map(url => { console.log(url) }))
// for each response show its status
.then(responses => { // (*)
    for(let response of responses) {
    alert(`${response.url}: ${response.status}`);
    }
});
*/
// -----------------------


// -----------------------
// CHAINING
// -----------------------
// TODO
// -----------------------
{'fraction_non_alphanumeric': 0.2463768115942029, 'fraction_numerical': 0.00966183574879227, 'mean_word_length': 1.9246704331450095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4011195', 'n_tokens_mistral': 916, 'n_tokens_neox': 883, 'n_words': 259}