/**
* @arg Array items
* @arg Async Function asyncFunction
* @return [Promise]
*/
function getInSequence(items, asyncFunction) {
return items.reduce((previous, item) => (
previous.then(accumulator => (
asyncFunction(item).then(result => accumulator.concat(result))
))
), Promise.resolve([]));
}Keywords: async, promise, best practice, use-case, sequence, reduce. Source example.
We have used type-check in this example, though any type checking library or self written check would do the trick.
const { typeCheck } = require('type-check');
const INPUT_TYPE = `{
urls: [String],
urlToTextFileWithUrls: Maybe String,
concurrency: Maybe Number,
storePagesInterval: Maybe Number,
screenshotWidth: Maybe Number,
screenshotHeight: Maybe Number
}`;
const input = await Apify.getValue('INPUT');
if (!typeCheck(INPUT_TYPE, input)) {
console.error('Expected input:');
console.error(INPUT_TYPE);
console.error('Received input:');
console.error(util.inspect(input));
throw new Error('Received invalid input');
}Keywords: best practice, use-case, input, error handling._ Source example.
All the methods allow the programmer to call another Act, getValue, setValue, with no previous client set-up or auth.
const Apify = {
main,
getEnv,
getValue,
setValue,
call,
readyFreddy,
setPromisesDependency,
getPromisesDependency,
browse,
launchWebDriver,
launchPuppeteer,
client: apifyClient,
events: new EventEmitter(),
};Keywords: use-case, methods, api, best practice.
Official Documentation. Source.