Skip to content

Instantly share code, notes, and snippets.

@thenoseman
Created June 9, 2017 09:27
Show Gist options
  • Save thenoseman/ad2d92d79b547e3f3617107adac1197e to your computer and use it in GitHub Desktop.
Save thenoseman/ad2d92d79b547e3f3617107adac1197e to your computer and use it in GitHub Desktop.

Revisions

  1. thenoseman created this gist Jun 9, 2017.
    30 changes: 30 additions & 0 deletions fof-example-multiple-findhtml.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    var rules = [{
    "name": "Fetching multiple thing from the content page with context.findHtml",
    "url": "https://scotch.io/tutorials/javascript-promises-for-dummies",
    "before": function(resolve, context) {
    // Lets fetch two headlines from the page
    // 1. Generate an array of Promises.
    var promises = [
    context.findHtml("#toc-understanding-promises"),
    context.findHtml("#toc-creating-a-promise")
    ];

    // Promise.all will resolve when ALL promises in the argument are resolved
    // You'll get an array in the .then() function containing one element per promise.
    Promise.all(promises).then(function(dataFromAllPromises) {
    // dataFromAllPromises[0] = result of context.findHtml("#toc-understanding-promises")
    // dataFromAllPromises[1] = result of context.findHtml("#toc-creating-a-promise")

    // Now transform the data (eg. remove tags) and resolve the "before" function to
    // make the data available in value functions.
    resolve(dataFromAllPromises);
    });
    },
    "fields": [{
    "selector": "#aa-search-input",
    "value": function($domElement, dataFrombeforeFunction) {
    return "#toc-understanding-promises : " + dataFrombeforeFunction[0] + " #toc-creating-a-promise : " + dataFrombeforeFunction[1];
    }
    }]
    }
    ];