javascript - How would I use an asynchronous function as a default parameter for another function -
i trying make function call function if parameter doesn't exist.
for example:
function getallfoo(){ // makes request api , returns array of foos } function getnumfoo(foosarray = getallfoo(), num = 5){ // selects num of foos foosarray or calls getallfoos selects num of them }
try wrap asynchronous function js promise, , in dependent function call then()
function:
function getallfoo () { return new promise( // resolver function called ability resolve or // reject promise function(resolve, reject) { // resolve or reject here, according logic var foosarray = ['your', 'array']; resolve(foosarray); } ) }; function getnumfoo(num = 5){ getallfoo().then(function (foosarray) { // selects num of foos foosarray or calls getallfoos selects num of them }); }
Comments
Post a Comment