Link to home
Start Free TrialLog in
Avatar of Michael Vasilevsky
Michael VasilevskyFlag for United States of America

asked on

Add Promise to Existing Code

I have the below code that is returning undefined. How to rewrite so it uses a promise or similar mechanism to ensure addTSRecord returns the id to TimesheetRecords?

TimesheetRecords[i].id = addTSRecord(TimesheetRecords[i].RowID, moment(myWE).subtract(6, 'd').toISOString(), TimesheetRecords[i].TSStaffId, TimesheetRecords[i].Hours, TimesheetRecords[i].TSTaskId);						

function addTSRecord(myRow, myDate, myTSStaffID, myHours, myTSTaskID) {

		var endpointUrl = mySite + "/_api/web/lists/getbytitle('timesheets')/items";
		
		call;

		var call = $.ajax({
    		url: endpointUrl,
    		type: "POST",
    		data: JSON.stringify({
    			"__metadata": { type: "SP.Data.TimesheetsListItem" },
    			RowID: myRow,
    			TSDate: myDate,
    			TSStaffId: myTSStaffID,
			Hours: myHours,
			TSTaskId: myTSTaskID
	        }),
    		headers: {
    			Accept: "application/json;odata=verbose",
    			"Content-Type": "application/json;odata=verbose",
    			"X-RequestDigest": myFormDigest
    		}
		});

		call.done(function (data, textStatus, jqXHR) {
			return data.d.Id;
		});

		call.fail(function (jqXHR, textStatus, errorThrown) {
    		var response = JSON.parse(jqXHR.responseText);
    		var message = response ? response.error.message.value : textStatus;
    		console.log(message);
		 });
		
	}	//	addTSRecord

Open in new window

Avatar of Michael Vasilevsky
Michael Vasilevsky
Flag of United States of America image

ASKER

Setting TimesheetRecords.id if what depends on addTSRecords being complete, so I could do something like the below?

var myID = addTSRecord(TimesheetRecords[i].RowID, moment(myWE).subtract(6, 'd').toISOString(), TimesheetRecords[i].TSStaffId, TimesheetRecords[i].Hours, TimesheetRecords[i].TSTaskId).promise().done(function() {
    TimesheetRecords[i].id = myID;
});

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Julian Hansen, option 2 looks like a good option, but TimesheetRecords.id is being set to an object, not the value of data.d.Id. Is there a way to force just the value?

Appreciate the help!

MV
Looking at your code again I see there is a typo - the promise call back has resp as the parameter yet we are accessing it with data

What is being returned?

If you do this - what do you get?
TimesheetRecords[i].id = addTSRecord(TimesheetRecords[i].RowID, moment(myWE).subtract(6, 'd').toISOString(), TimesheetRecords[i].TSStaffId, TimesheetRecords[i].Hours, TimesheetRecords[i].TSTaskId).then(function(resp) {
    console.log(resp);
});

Open in new window

Yes needed to replace data with resp.

It appears to be working with the below. Thank you!

TimesheetRecords[i].id = addTSRecord(TimesheetRecords[i].RowID, moment(myWE).subtract(6, 'd').toISOString(), TimesheetRecords[i].TSStaffId, TimesheetRecords[i].Hours, TimesheetRecords[i].TSTaskId).then(function(resp) {
    TimesheetRecords[i - 1].id = resp.d.Id;
});						

function addTSRecord(myRow, myDate, myTSStaffID, myHours, myTSTaskID) {

		var endpointUrl = mySite + "/_api/web/lists/getbytitle('timesheets')/items";
		
		return $.ajax({
    		url: endpointUrl,
    		type: "POST",
    		data: JSON.stringify({
    			"__metadata": { type: "SP.Data.TimesheetsListItem" },
    			RowID: myRow,
    			TSDate: myDate,
    			TSStaffId: myTSStaffID,
			Hours: myHours,
			TSTaskId: myTSTaskID
	        }),
    		headers: {
    			Accept: "application/json;odata=verbose",
    			"Content-Type": "application/json;odata=verbose",
    			"X-RequestDigest": myFormDigest
    		}
		});
                return call;
}

Open in new window

You are welcome.