i trying use promises loops , nested functions within of loops. have series of functions supposed bring sharepoint list items rest calls - once done executing, function gets called uses data brought back.
since there multiple lists, , subsequently multiple list items in each, used while loop make each rest call, , there, data (list items) put objects. objects gets placed array , that's second functions uses continue on.
i'm having trouble receiving responses promises. thinking have multiple promises pushed array, use promise.all
see if resolved before using then
. problem promises stay pending
since i'm not returning resolve
correctly. please see below.
function onquerysuccess(sender, args) { var itemenumerator = items.getenumerator(); while (itemenumerator.movenext()) { var promise = new promise(function (resolve, reject) { var item = itemenumerator.get_current(); item = item.get_item('url'); var itemurl = item.get_description(); getrequestitemsfromlist(itemurl); }); promises.push(promise); // promises present, status pending } console.log(promises); promise.all(promises).then(function (val) { console.log(val); execfuncs(); // function execute once above done }).catch(function (response) { console.log(response); }); }
because there's lot of functions involved, order of execution:
getrequestitemsfromlist //gets url each list execcrossdomainrequest (on success call) // makes rest call list , items cleandata // trims data , puts in objects
the last figured call promise.resolve()
since that's end of line.
either way, that's not working. checked out other threads, i'm trying without using libraries. in advance.
edit:
full relevant code:
var promises = []; window.requests = []; function getrequestlists() { var requestslists = hostweb.get_lists().getbytitle('name'); // sharepoint list request list urls. context.load(requestslists); var camlquery = new sp.camlquery(); camlquery.set_viewxml('<view></view>'); var items = requestslists.getitems(camlquery); context.load(items, 'include(url)'); context.executequeryasync(onquerysuccess, onqueryfail); function onquerysuccess(sender, args) { var itemenumerator = items.getenumerator(); while (itemenumerator.movenext()) { var promise = new promise(function (resolve, reject) { var item = itemenumerator.get_current(); item = item.get_item('url'); var itemurl = item.get_description(); getrequestitemsfromlist(itemurl); }); promises.push(promise); } console.log(promises); promise.all(promises).then(function (val) { console.log(val); execfuncs(); // not shown here }).catch(function (response) { console.log(response); }); } function onqueryfail(sender, args) { alert("request retrieve list url items has failed. " + args.get_message()); } } function getrequestitemsfromlist(url) { var lastpos = getsubstringindex(url, "/", 4); var weburl = url.substring(0, lastpos); // truncates list url parse out web url var abslistpos = getsubstringindex(url, "allitems.aspx", 1); var abslisturl = url.substring(0, abslistpos); // truncates allitems.aspx @ end of list url var rellistpos = getsubstringindex(abslisturl, "/", 3); var rellisturl = abslisturl.substring(rellistpos, abslisturl.length); // gets list's relative url var listname = "list name"; console.log(weburl); execcrossdomainrequest(weburl, listname, abslisturl); } function execcrossdomainrequest(weburl, listname, abslisturl) { var executor = new sp.requestexecutor(appweburl); executor.executeasync({ // collect list description url: appweburl + "/_api/sp.appcontextsite(@target)/web/lists/getbytitle(@name)?" + "@target='" + weburl + "'&@name='" + listname + "'" + "&$select=description", method: "get", headers: { "accept": "application/json; odata=verbose" }, success: oncallsuccess, error: oncallfail }); function oncallsuccess(data) { var json = json.parse(data.body); var description = json.d.description; executor.executeasync({ // collect list item information url: appweburl + "/_api/sp.appcontextsite(@target)/web/lists/getbytitle(@name)/items?" + "@target='" + weburl + "'&@name='" + listname + "'" + "&$top=500&$select=*," + "assigned_x0020_to/title" + "&$expand=assigned_x0020_to/id", method: "get", headers: { "accept": "application/json; odata=verbose" }, success: onitemscallsuccess, error: onitemscallfail }); function onitemscallsuccess(data) { var itemsjson = json.parse(data.body); var results = itemsjson.d.results; cleandata(results, description, abslisturl); } function onitemscallfail(data, errorcode, errormessage) { console.log("could not make list items cross domain call. " + errormessage); } } function oncallfail(data, errorcode, errormessage) { console.log("could not make list cross domain call. " + errormessage); } } function cleandata(results, listdescription, abslisturl) { if (!results.length) { return; } (var = 0; < results.length; i++) { var client = listdescription; var id = results[i].id; ... } var request = new request(client, id, path, title, status, estimated, assignedto, priority, description, response); window.requests.push(request); } return promise.resolve(); }
when use promise constructor this:
var promise = new promise(function (resolve, reject) {
it implies somewhere inside block calling resolve
and/or reject
, settle promise.
but never doing it, leaving created promise object in pending state forever.
see constructing promise.
Comments
Post a Comment