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.
Form-O-Fill example : Using multiple context.findHtml() calls in before functions
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];
}
}]
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment