Sequential Server.callWithGET uses?
-
Can we use sequential Server.callWithGET commands one after another? I need this because procedure is:
- First, pushing email to the server, then first response comes
- Then, taking the user id of that email, inside the first response
- Then using this id to make another request (with combining other strings)
- Then second response comes
Below procedure is not working.
inline function onButton1Control(component, value) { if(value) { Server.callWithGET("API Link", "", function(status, response) { }); local endpoint1 = "String1"; local usid = response.id; local endpoint2 = "String2"; local totalEndPoint = endpoint1 + usid + endpoint2; Server.callWithGET(totalEndPoint, "", function(status, response) { }); } }; Content.getComponent("Button1").setControlCallback(onButton1Control);
-
You need to use nested callbacks.
-
Yes David is right and in order to avoid getting spaghetti code, you need to use named functions like this:
function firstCall(status, response) { var endpoint1 = "String1"; var usid = response.id; var endpoint2 = "String2"; var totalEndPoint = endpoint1 + usid + endpoint2; Server.callWithGET(totalEndPoint, "", secondCall); } function secondCall(status, response) { // continue here... } inline function onButton1Control(component, value) { if(value) Server.callWithGET("API Link", "", firstCall); }; Content.getComponent("Button1").setControlCallback(onButton1Control);
-
@Christoph-Hart @d-healey
Looks very cool, Thank you! -
@Christoph-Hart said in Sequential Server.callWithGET uses?:
Yes David is right and in order to avoid getting spaghetti code, you need to use named functions like this:
function firstCall(status, response) { var endpoint1 = "String1"; var usid = response.id; var endpoint2 = "String2"; var totalEndPoint = endpoint1 + usid + endpoint2; Server.callWithGET(totalEndPoint, "", secondCall); } function secondCall(status, response) { // continue here... } inline function onButton1Control(component, value) { if(value) Server.callWithGET("API Link", "", firstCall); }; Content.getComponent("Button1").setControlCallback(onButton1Control);
firstCall response is ok and I can print trace(response) data.
But
var usid = response.id;
looks undefined. What am I missing? -
@orange I think this is just an example. Your response object probably doesn't have an id field.
-
@d-healey Ok you are right, it doesn't have directly an id field. Woocommerce user retrieve response is in an array format. So:
var usid = response[0].id;
works.[ { "id": 12345, "date_created": "", "date_created_gmt": "", "date_modified": "", "date_modified_gmt": "", "email": "USER_EMAIL", "first_name": "", "last_name": "", "role": "customer", "username": "", "billing": { "first_name": "", "last_name": "", "company": "", "address_1": "", "address_2": "", "city": "", "postcode": "", "country": "", "state": "", "email": "", "phone": "" }, "shipping": { "first_name": "", "last_name": "", "company": "", "address_1": "", "address_2": "", "city": "", "postcode": "", "country": "", "state": "" }, "is_paying_customer": true, "avatar_url": "", "meta_data": [ { "id": , "key": "", "value": "" }, { "id": , "key": "", "value": "" } ], "_links": { "self": [ { "href": "" } ], "collection": [ { "href": "" } ] } } ]