How to async await with jQuery sortable receive and update?How do I check if an element is hidden in jQuery?How to manage a redirect request after a jQuery Ajax callHow can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How can I select an element with multiple classes in jQuery?How can I refresh a page with jQuery?How can I update NodeJS and NPM to the next versions?How and when to use ‘async’ and ‘await’How do I update each dependency in package.json to the latest version?Using async/await with a forEach loop

Is the U.S. Code copyrighted by the Government?

When were female captains banned from Starfleet?

How do I color the graph in datavisualization?

What should you do when eye contact makes your subordinate uncomfortable?

What is this called? Old film camera viewer?

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

How much character growth crosses the line into breaking the character

Where does the bonus feat in the cleric starting package come from?

Why did the EU agree to delay the Brexit deadline?

Longest common substring in linear time

How to bake one texture for one mesh with multiple textures blender 2.8

Problem with TransformedDistribution

Creature in Shazam mid-credits scene?

What should you do if you miss a job interview (deliberately)?

It grows, but water kills it

Melting point of aspirin, contradicting sources

A social experiment. What is the worst that can happen?

Multiplicative persistence

The IT department bottlenecks progress. How should I handle this?

WiFi Thermostat, No C Terminal on Furnace

Why Shazam when there is already Superman?

Is it improper etiquette to ask your opponent what his/her rating is before the game?

Should I outline or discovery write my stories?



How to async await with jQuery sortable receive and update?


How do I check if an element is hidden in jQuery?How to manage a redirect request after a jQuery Ajax callHow can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How can I select an element with multiple classes in jQuery?How can I refresh a page with jQuery?How can I update NodeJS and NPM to the next versions?How and when to use ‘async’ and ‘await’How do I update each dependency in package.json to the latest version?Using async/await with a forEach loop













1















I use sortable's receive to receive elements from a draggable list. When this happens, the element's data is sent to the server using an Ajax call and an entry is created in the database. Once done, an id gets returned and added to the new element.



I also have an update function for the same sortable list, where upon any changes, it loops through the list to grab ids and makes another Ajax call for updating the values.



Problem is that the processes get mixed and the update function grabs all the ids before the new element's id is loaded, meaning it will be short of the new element's id.



$( ".sort1" ).sortable({
connectWith: ".sortable-sections",
stack: ".sortable-sections ul",
cancel: "input,textarea,button,select,option,[contenteditable],.editorArea,.unsortable",
receive: function(event, ui)
// lots of things happen
createNewComponentInDB(data);
// leads to the ajax post which if successful $(newItem).attr('id', response);
,
update: function(event, ui)
// grab list items indexes and ids
// ajax post to update in database



What can I do to get the functions in 'receive' to finish before moving on to functions in 'update'? Or is there a better way to do this?



Edit:



Sample HTML:



<ul class="space-sections sortable-sections sort1 ui-sortable"> 
<li class="space-section drag draggable-item ui-sortable-handle" id="5c920e65a7917045fbf62d45" data-component-type="list">
/* ... lots of nested stuff */
</li>
<li class="space-section drag draggable-item ui-sortable-handle" id="5c920e6ca7917045fbf62d46" data-component-type="list">
/* ... lots of nested stuff */
</li>
</ul>


UPDATE WITH SOLUTION:



Thanks to a conversation with @elem4th, I was able to solve this by using the 'flag concept'.



  1. I created a variable receiveInProgress and set it to false.


  2. Then turned the functions inside 'update' into a standalone function updateComponentOrderInDB()



  3. Inside 'update'



     if (receiveInProgress == true) 
    console.log("Receive in progress! Update will happen on its own.")
    else
    updateComponentOrderInDB();



  4. Now, at the beginning of 'receive', I set receiveInProgress to true, and at the end of the Ajax call, I set it back to false and run updateComponentOrderInDB(). Now all works in desired order!










share|improve this question
























  • Can you post a sample html for this? Also, async-await is effectively the same thing as a callback, so it wouldn't matter in this situation.

    – Jane
    2 days ago











  • @Jane Do you mean the sortable list with its elements? Made an edit.

    – robertunyx
    2 days ago















1















I use sortable's receive to receive elements from a draggable list. When this happens, the element's data is sent to the server using an Ajax call and an entry is created in the database. Once done, an id gets returned and added to the new element.



I also have an update function for the same sortable list, where upon any changes, it loops through the list to grab ids and makes another Ajax call for updating the values.



Problem is that the processes get mixed and the update function grabs all the ids before the new element's id is loaded, meaning it will be short of the new element's id.



$( ".sort1" ).sortable({
connectWith: ".sortable-sections",
stack: ".sortable-sections ul",
cancel: "input,textarea,button,select,option,[contenteditable],.editorArea,.unsortable",
receive: function(event, ui)
// lots of things happen
createNewComponentInDB(data);
// leads to the ajax post which if successful $(newItem).attr('id', response);
,
update: function(event, ui)
// grab list items indexes and ids
// ajax post to update in database



What can I do to get the functions in 'receive' to finish before moving on to functions in 'update'? Or is there a better way to do this?



Edit:



Sample HTML:



<ul class="space-sections sortable-sections sort1 ui-sortable"> 
<li class="space-section drag draggable-item ui-sortable-handle" id="5c920e65a7917045fbf62d45" data-component-type="list">
/* ... lots of nested stuff */
</li>
<li class="space-section drag draggable-item ui-sortable-handle" id="5c920e6ca7917045fbf62d46" data-component-type="list">
/* ... lots of nested stuff */
</li>
</ul>


UPDATE WITH SOLUTION:



Thanks to a conversation with @elem4th, I was able to solve this by using the 'flag concept'.



  1. I created a variable receiveInProgress and set it to false.


  2. Then turned the functions inside 'update' into a standalone function updateComponentOrderInDB()



  3. Inside 'update'



     if (receiveInProgress == true) 
    console.log("Receive in progress! Update will happen on its own.")
    else
    updateComponentOrderInDB();



  4. Now, at the beginning of 'receive', I set receiveInProgress to true, and at the end of the Ajax call, I set it back to false and run updateComponentOrderInDB(). Now all works in desired order!










share|improve this question
























  • Can you post a sample html for this? Also, async-await is effectively the same thing as a callback, so it wouldn't matter in this situation.

    – Jane
    2 days ago











  • @Jane Do you mean the sortable list with its elements? Made an edit.

    – robertunyx
    2 days ago













1












1








1








I use sortable's receive to receive elements from a draggable list. When this happens, the element's data is sent to the server using an Ajax call and an entry is created in the database. Once done, an id gets returned and added to the new element.



I also have an update function for the same sortable list, where upon any changes, it loops through the list to grab ids and makes another Ajax call for updating the values.



Problem is that the processes get mixed and the update function grabs all the ids before the new element's id is loaded, meaning it will be short of the new element's id.



$( ".sort1" ).sortable({
connectWith: ".sortable-sections",
stack: ".sortable-sections ul",
cancel: "input,textarea,button,select,option,[contenteditable],.editorArea,.unsortable",
receive: function(event, ui)
// lots of things happen
createNewComponentInDB(data);
// leads to the ajax post which if successful $(newItem).attr('id', response);
,
update: function(event, ui)
// grab list items indexes and ids
// ajax post to update in database



What can I do to get the functions in 'receive' to finish before moving on to functions in 'update'? Or is there a better way to do this?



Edit:



Sample HTML:



<ul class="space-sections sortable-sections sort1 ui-sortable"> 
<li class="space-section drag draggable-item ui-sortable-handle" id="5c920e65a7917045fbf62d45" data-component-type="list">
/* ... lots of nested stuff */
</li>
<li class="space-section drag draggable-item ui-sortable-handle" id="5c920e6ca7917045fbf62d46" data-component-type="list">
/* ... lots of nested stuff */
</li>
</ul>


UPDATE WITH SOLUTION:



Thanks to a conversation with @elem4th, I was able to solve this by using the 'flag concept'.



  1. I created a variable receiveInProgress and set it to false.


  2. Then turned the functions inside 'update' into a standalone function updateComponentOrderInDB()



  3. Inside 'update'



     if (receiveInProgress == true) 
    console.log("Receive in progress! Update will happen on its own.")
    else
    updateComponentOrderInDB();



  4. Now, at the beginning of 'receive', I set receiveInProgress to true, and at the end of the Ajax call, I set it back to false and run updateComponentOrderInDB(). Now all works in desired order!










share|improve this question
















I use sortable's receive to receive elements from a draggable list. When this happens, the element's data is sent to the server using an Ajax call and an entry is created in the database. Once done, an id gets returned and added to the new element.



I also have an update function for the same sortable list, where upon any changes, it loops through the list to grab ids and makes another Ajax call for updating the values.



Problem is that the processes get mixed and the update function grabs all the ids before the new element's id is loaded, meaning it will be short of the new element's id.



$( ".sort1" ).sortable({
connectWith: ".sortable-sections",
stack: ".sortable-sections ul",
cancel: "input,textarea,button,select,option,[contenteditable],.editorArea,.unsortable",
receive: function(event, ui)
// lots of things happen
createNewComponentInDB(data);
// leads to the ajax post which if successful $(newItem).attr('id', response);
,
update: function(event, ui)
// grab list items indexes and ids
// ajax post to update in database



What can I do to get the functions in 'receive' to finish before moving on to functions in 'update'? Or is there a better way to do this?



Edit:



Sample HTML:



<ul class="space-sections sortable-sections sort1 ui-sortable"> 
<li class="space-section drag draggable-item ui-sortable-handle" id="5c920e65a7917045fbf62d45" data-component-type="list">
/* ... lots of nested stuff */
</li>
<li class="space-section drag draggable-item ui-sortable-handle" id="5c920e6ca7917045fbf62d46" data-component-type="list">
/* ... lots of nested stuff */
</li>
</ul>


UPDATE WITH SOLUTION:



Thanks to a conversation with @elem4th, I was able to solve this by using the 'flag concept'.



  1. I created a variable receiveInProgress and set it to false.


  2. Then turned the functions inside 'update' into a standalone function updateComponentOrderInDB()



  3. Inside 'update'



     if (receiveInProgress == true) 
    console.log("Receive in progress! Update will happen on its own.")
    else
    updateComponentOrderInDB();



  4. Now, at the beginning of 'receive', I set receiveInProgress to true, and at the end of the Ajax call, I set it back to false and run updateComponentOrderInDB(). Now all works in desired order!







jquery node.js ajax async-await jquery-ui-sortable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago







robertunyx

















asked 2 days ago









robertunyxrobertunyx

236




236












  • Can you post a sample html for this? Also, async-await is effectively the same thing as a callback, so it wouldn't matter in this situation.

    – Jane
    2 days ago











  • @Jane Do you mean the sortable list with its elements? Made an edit.

    – robertunyx
    2 days ago

















  • Can you post a sample html for this? Also, async-await is effectively the same thing as a callback, so it wouldn't matter in this situation.

    – Jane
    2 days ago











  • @Jane Do you mean the sortable list with its elements? Made an edit.

    – robertunyx
    2 days ago
















Can you post a sample html for this? Also, async-await is effectively the same thing as a callback, so it wouldn't matter in this situation.

– Jane
2 days ago





Can you post a sample html for this? Also, async-await is effectively the same thing as a callback, so it wouldn't matter in this situation.

– Jane
2 days ago













@Jane Do you mean the sortable list with its elements? Made an edit.

– robertunyx
2 days ago





@Jane Do you mean the sortable list with its elements? Made an edit.

– robertunyx
2 days ago












1 Answer
1






active

oldest

votes


















2














The 'receive' and 'update' callbacks are called synchronously by the sortable widget. It is not possible to 'wait' for async calls in receive function to complete before the 'update' fires.



You should approach this differently. You probably don't need the 'update' function at all. You can trigger your 'sort' in createNewComponentInDB function, after the Ajax call is complete. I am assuming you are executing $(newItem).attr('id', response); in callback of your Ajax call, simply put your sorting logic immediately after this line.






share|improve this answer























  • True, that would work, but then upon regular reordering of sortable items (in cases when I'm not creating new elements through draggable), I wouldn't be able to update the order in database. A fix could be to periodically grab the items and carry out a database update. Although then, just by coincidence, this order process might occur at the same time as the dragging happens.

    – robertunyx
    2 days ago












  • Right. Well, then you do need the 'update' function too, since update events can be fired separately too. One option would be to set a flag before you trigger Ajax, reset it after Ajax call is complete. In update function if the flag is set, don't do anything assuming 'receive' is in progress and it will sort at end of Ajax call. Otherwise continue.

    – elem4th
    2 days ago












  • OH WOW! That is absolutely brilliant! Thank you!!! I updated my answer with how I implemented the suggestion.

    – robertunyx
    2 days ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281407%2fhow-to-async-await-with-jquery-sortable-receive-and-update%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The 'receive' and 'update' callbacks are called synchronously by the sortable widget. It is not possible to 'wait' for async calls in receive function to complete before the 'update' fires.



You should approach this differently. You probably don't need the 'update' function at all. You can trigger your 'sort' in createNewComponentInDB function, after the Ajax call is complete. I am assuming you are executing $(newItem).attr('id', response); in callback of your Ajax call, simply put your sorting logic immediately after this line.






share|improve this answer























  • True, that would work, but then upon regular reordering of sortable items (in cases when I'm not creating new elements through draggable), I wouldn't be able to update the order in database. A fix could be to periodically grab the items and carry out a database update. Although then, just by coincidence, this order process might occur at the same time as the dragging happens.

    – robertunyx
    2 days ago












  • Right. Well, then you do need the 'update' function too, since update events can be fired separately too. One option would be to set a flag before you trigger Ajax, reset it after Ajax call is complete. In update function if the flag is set, don't do anything assuming 'receive' is in progress and it will sort at end of Ajax call. Otherwise continue.

    – elem4th
    2 days ago












  • OH WOW! That is absolutely brilliant! Thank you!!! I updated my answer with how I implemented the suggestion.

    – robertunyx
    2 days ago















2














The 'receive' and 'update' callbacks are called synchronously by the sortable widget. It is not possible to 'wait' for async calls in receive function to complete before the 'update' fires.



You should approach this differently. You probably don't need the 'update' function at all. You can trigger your 'sort' in createNewComponentInDB function, after the Ajax call is complete. I am assuming you are executing $(newItem).attr('id', response); in callback of your Ajax call, simply put your sorting logic immediately after this line.






share|improve this answer























  • True, that would work, but then upon regular reordering of sortable items (in cases when I'm not creating new elements through draggable), I wouldn't be able to update the order in database. A fix could be to periodically grab the items and carry out a database update. Although then, just by coincidence, this order process might occur at the same time as the dragging happens.

    – robertunyx
    2 days ago












  • Right. Well, then you do need the 'update' function too, since update events can be fired separately too. One option would be to set a flag before you trigger Ajax, reset it after Ajax call is complete. In update function if the flag is set, don't do anything assuming 'receive' is in progress and it will sort at end of Ajax call. Otherwise continue.

    – elem4th
    2 days ago












  • OH WOW! That is absolutely brilliant! Thank you!!! I updated my answer with how I implemented the suggestion.

    – robertunyx
    2 days ago













2












2








2







The 'receive' and 'update' callbacks are called synchronously by the sortable widget. It is not possible to 'wait' for async calls in receive function to complete before the 'update' fires.



You should approach this differently. You probably don't need the 'update' function at all. You can trigger your 'sort' in createNewComponentInDB function, after the Ajax call is complete. I am assuming you are executing $(newItem).attr('id', response); in callback of your Ajax call, simply put your sorting logic immediately after this line.






share|improve this answer













The 'receive' and 'update' callbacks are called synchronously by the sortable widget. It is not possible to 'wait' for async calls in receive function to complete before the 'update' fires.



You should approach this differently. You probably don't need the 'update' function at all. You can trigger your 'sort' in createNewComponentInDB function, after the Ajax call is complete. I am assuming you are executing $(newItem).attr('id', response); in callback of your Ajax call, simply put your sorting logic immediately after this line.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









elem4thelem4th

71737




71737












  • True, that would work, but then upon regular reordering of sortable items (in cases when I'm not creating new elements through draggable), I wouldn't be able to update the order in database. A fix could be to periodically grab the items and carry out a database update. Although then, just by coincidence, this order process might occur at the same time as the dragging happens.

    – robertunyx
    2 days ago












  • Right. Well, then you do need the 'update' function too, since update events can be fired separately too. One option would be to set a flag before you trigger Ajax, reset it after Ajax call is complete. In update function if the flag is set, don't do anything assuming 'receive' is in progress and it will sort at end of Ajax call. Otherwise continue.

    – elem4th
    2 days ago












  • OH WOW! That is absolutely brilliant! Thank you!!! I updated my answer with how I implemented the suggestion.

    – robertunyx
    2 days ago

















  • True, that would work, but then upon regular reordering of sortable items (in cases when I'm not creating new elements through draggable), I wouldn't be able to update the order in database. A fix could be to periodically grab the items and carry out a database update. Although then, just by coincidence, this order process might occur at the same time as the dragging happens.

    – robertunyx
    2 days ago












  • Right. Well, then you do need the 'update' function too, since update events can be fired separately too. One option would be to set a flag before you trigger Ajax, reset it after Ajax call is complete. In update function if the flag is set, don't do anything assuming 'receive' is in progress and it will sort at end of Ajax call. Otherwise continue.

    – elem4th
    2 days ago












  • OH WOW! That is absolutely brilliant! Thank you!!! I updated my answer with how I implemented the suggestion.

    – robertunyx
    2 days ago
















True, that would work, but then upon regular reordering of sortable items (in cases when I'm not creating new elements through draggable), I wouldn't be able to update the order in database. A fix could be to periodically grab the items and carry out a database update. Although then, just by coincidence, this order process might occur at the same time as the dragging happens.

– robertunyx
2 days ago






True, that would work, but then upon regular reordering of sortable items (in cases when I'm not creating new elements through draggable), I wouldn't be able to update the order in database. A fix could be to periodically grab the items and carry out a database update. Although then, just by coincidence, this order process might occur at the same time as the dragging happens.

– robertunyx
2 days ago














Right. Well, then you do need the 'update' function too, since update events can be fired separately too. One option would be to set a flag before you trigger Ajax, reset it after Ajax call is complete. In update function if the flag is set, don't do anything assuming 'receive' is in progress and it will sort at end of Ajax call. Otherwise continue.

– elem4th
2 days ago






Right. Well, then you do need the 'update' function too, since update events can be fired separately too. One option would be to set a flag before you trigger Ajax, reset it after Ajax call is complete. In update function if the flag is set, don't do anything assuming 'receive' is in progress and it will sort at end of Ajax call. Otherwise continue.

– elem4th
2 days ago














OH WOW! That is absolutely brilliant! Thank you!!! I updated my answer with how I implemented the suggestion.

– robertunyx
2 days ago





OH WOW! That is absolutely brilliant! Thank you!!! I updated my answer with how I implemented the suggestion.

– robertunyx
2 days ago



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281407%2fhow-to-async-await-with-jquery-sortable-receive-and-update%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현