dont wait until function finishes to run another function









up vote
0
down vote

favorite












Hello I am creating this script where I create a post request that adds an item to cart and selects the size all in the backend.



As it runs it opens up a loading html and I wanted to have the current item that is being added's info added into a table in the html page.



Im not sure if this is the most efficent and fast method (please let me know if there is a better way) but what I did was I create a listener on the html/js page and send a message from the background.js page that is doing the work to that js page that is listening for the message.



What it does is that It finds the item so it sends a message of the item name, then it finds the items proper color and sends a message of the color selected, then it finds the size and selects the proper size. here is the code:



chrome.runtime.sendMessage( carting: true, status: 
status_item: (item[0]) //item name
);

//carting code(irrelevant to this issue)

chrome.runtime.sendMessage( carting: true, status:
status_color: (item[1]) //item color
);

//more irrelevent carting code

chrome.runtime.sendMessage( carting: true, status:
status_size: (size_text.text) // size
);


this is what I had in the listener page:



chrome.runtime.onMessage.addListener(function(message, sender) 
if (!message.carting) return;

var Status = message.status;

$(function()

$("#tasks_table").append('<tr>'
+'<td>'+item+'</td>'
+'<td>'+color+'</td>'
+'<td>'+size+'</td>'
+'</tr>');




)


The problem with it was is that during the carting process when it yet hasn't found the color it would just constantly add undefined into the table so I found a solution to that issue by using functions:



function waitForItem(item) 
if (typeof item !== "undefined")
$("#tasks_table").append('<tr>'+'<td>'+item+'</td>');
else
setTimeout(waitForItem, 10);



function waitForColor(color)
if (typeof color !== "undefined")
$("#tasks_table").append('<td>'+color+'</td>');
else
setTimeout(waitForColor, 10);



function waitForSize(size)
if (typeof size !== "undefined")
$("#tasks_table").append('<td>'+size+'</td>'+'</tr>');
else
setTimeout(waitForSize, 10);


waitForItem(item);
waitForColor(color);
waitForSize(size);


Now this did work it stopped adding the undefined into the table but now the problem is that it runs the first item function until it is completly done and then so on with each function.



When I have two items it adds the item name for the first item into the table then waits until the second item name is found and adds it too then proceeds to add all the color and the sizes. The problem with this is that my background page works in a loop. It finds the first item, then its color, then its size, then it goes onto the next item, its color, and its size.



So when It finds the first item and then the second item it adds all the rest of the color and size right at the same time. Which I don't want.



I want it to add the info into the table the same way the background page runs so the user can see how smoothly and fast it runs.










share|improve this question























  • I don't see the need for messaging at all (I think you could do everything right in the content script), but if you want to sequentially execute asynchronous functions such as chrome API methods, you must invoke the next one from a callback of a preceding one and so on, or promisify the API (Using Mozilla WebExtension polyfill for example) and chain via .then() or async/await keywords.
    – wOxxOm
    Nov 10 at 8:20










  • @wOxxOm How would I do it inside the background.js script? because Ive tried it and couldn't figure out how.
    – developer12
    Nov 10 at 8:36










  • "It" - what? I don't even know why you need a background script for this task. As for asynchronous JavaScript (Promise, async/await, etc) you can find a lot of tutorials.
    – wOxxOm
    Nov 10 at 8:49










  • no the background script does all the carting and I send the messages from it to the html loading page can I insert the text into the html page without sending messages? @wOxxOm
    – developer12
    Nov 10 at 9:15










  • Usually you can do it directly.
    – wOxxOm
    Nov 10 at 9:39














up vote
0
down vote

favorite












Hello I am creating this script where I create a post request that adds an item to cart and selects the size all in the backend.



As it runs it opens up a loading html and I wanted to have the current item that is being added's info added into a table in the html page.



Im not sure if this is the most efficent and fast method (please let me know if there is a better way) but what I did was I create a listener on the html/js page and send a message from the background.js page that is doing the work to that js page that is listening for the message.



What it does is that It finds the item so it sends a message of the item name, then it finds the items proper color and sends a message of the color selected, then it finds the size and selects the proper size. here is the code:



chrome.runtime.sendMessage( carting: true, status: 
status_item: (item[0]) //item name
);

//carting code(irrelevant to this issue)

chrome.runtime.sendMessage( carting: true, status:
status_color: (item[1]) //item color
);

//more irrelevent carting code

chrome.runtime.sendMessage( carting: true, status:
status_size: (size_text.text) // size
);


this is what I had in the listener page:



chrome.runtime.onMessage.addListener(function(message, sender) 
if (!message.carting) return;

var Status = message.status;

$(function()

$("#tasks_table").append('<tr>'
+'<td>'+item+'</td>'
+'<td>'+color+'</td>'
+'<td>'+size+'</td>'
+'</tr>');




)


The problem with it was is that during the carting process when it yet hasn't found the color it would just constantly add undefined into the table so I found a solution to that issue by using functions:



function waitForItem(item) 
if (typeof item !== "undefined")
$("#tasks_table").append('<tr>'+'<td>'+item+'</td>');
else
setTimeout(waitForItem, 10);



function waitForColor(color)
if (typeof color !== "undefined")
$("#tasks_table").append('<td>'+color+'</td>');
else
setTimeout(waitForColor, 10);



function waitForSize(size)
if (typeof size !== "undefined")
$("#tasks_table").append('<td>'+size+'</td>'+'</tr>');
else
setTimeout(waitForSize, 10);


waitForItem(item);
waitForColor(color);
waitForSize(size);


Now this did work it stopped adding the undefined into the table but now the problem is that it runs the first item function until it is completly done and then so on with each function.



When I have two items it adds the item name for the first item into the table then waits until the second item name is found and adds it too then proceeds to add all the color and the sizes. The problem with this is that my background page works in a loop. It finds the first item, then its color, then its size, then it goes onto the next item, its color, and its size.



So when It finds the first item and then the second item it adds all the rest of the color and size right at the same time. Which I don't want.



I want it to add the info into the table the same way the background page runs so the user can see how smoothly and fast it runs.










share|improve this question























  • I don't see the need for messaging at all (I think you could do everything right in the content script), but if you want to sequentially execute asynchronous functions such as chrome API methods, you must invoke the next one from a callback of a preceding one and so on, or promisify the API (Using Mozilla WebExtension polyfill for example) and chain via .then() or async/await keywords.
    – wOxxOm
    Nov 10 at 8:20










  • @wOxxOm How would I do it inside the background.js script? because Ive tried it and couldn't figure out how.
    – developer12
    Nov 10 at 8:36










  • "It" - what? I don't even know why you need a background script for this task. As for asynchronous JavaScript (Promise, async/await, etc) you can find a lot of tutorials.
    – wOxxOm
    Nov 10 at 8:49










  • no the background script does all the carting and I send the messages from it to the html loading page can I insert the text into the html page without sending messages? @wOxxOm
    – developer12
    Nov 10 at 9:15










  • Usually you can do it directly.
    – wOxxOm
    Nov 10 at 9:39












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Hello I am creating this script where I create a post request that adds an item to cart and selects the size all in the backend.



As it runs it opens up a loading html and I wanted to have the current item that is being added's info added into a table in the html page.



Im not sure if this is the most efficent and fast method (please let me know if there is a better way) but what I did was I create a listener on the html/js page and send a message from the background.js page that is doing the work to that js page that is listening for the message.



What it does is that It finds the item so it sends a message of the item name, then it finds the items proper color and sends a message of the color selected, then it finds the size and selects the proper size. here is the code:



chrome.runtime.sendMessage( carting: true, status: 
status_item: (item[0]) //item name
);

//carting code(irrelevant to this issue)

chrome.runtime.sendMessage( carting: true, status:
status_color: (item[1]) //item color
);

//more irrelevent carting code

chrome.runtime.sendMessage( carting: true, status:
status_size: (size_text.text) // size
);


this is what I had in the listener page:



chrome.runtime.onMessage.addListener(function(message, sender) 
if (!message.carting) return;

var Status = message.status;

$(function()

$("#tasks_table").append('<tr>'
+'<td>'+item+'</td>'
+'<td>'+color+'</td>'
+'<td>'+size+'</td>'
+'</tr>');




)


The problem with it was is that during the carting process when it yet hasn't found the color it would just constantly add undefined into the table so I found a solution to that issue by using functions:



function waitForItem(item) 
if (typeof item !== "undefined")
$("#tasks_table").append('<tr>'+'<td>'+item+'</td>');
else
setTimeout(waitForItem, 10);



function waitForColor(color)
if (typeof color !== "undefined")
$("#tasks_table").append('<td>'+color+'</td>');
else
setTimeout(waitForColor, 10);



function waitForSize(size)
if (typeof size !== "undefined")
$("#tasks_table").append('<td>'+size+'</td>'+'</tr>');
else
setTimeout(waitForSize, 10);


waitForItem(item);
waitForColor(color);
waitForSize(size);


Now this did work it stopped adding the undefined into the table but now the problem is that it runs the first item function until it is completly done and then so on with each function.



When I have two items it adds the item name for the first item into the table then waits until the second item name is found and adds it too then proceeds to add all the color and the sizes. The problem with this is that my background page works in a loop. It finds the first item, then its color, then its size, then it goes onto the next item, its color, and its size.



So when It finds the first item and then the second item it adds all the rest of the color and size right at the same time. Which I don't want.



I want it to add the info into the table the same way the background page runs so the user can see how smoothly and fast it runs.










share|improve this question















Hello I am creating this script where I create a post request that adds an item to cart and selects the size all in the backend.



As it runs it opens up a loading html and I wanted to have the current item that is being added's info added into a table in the html page.



Im not sure if this is the most efficent and fast method (please let me know if there is a better way) but what I did was I create a listener on the html/js page and send a message from the background.js page that is doing the work to that js page that is listening for the message.



What it does is that It finds the item so it sends a message of the item name, then it finds the items proper color and sends a message of the color selected, then it finds the size and selects the proper size. here is the code:



chrome.runtime.sendMessage( carting: true, status: 
status_item: (item[0]) //item name
);

//carting code(irrelevant to this issue)

chrome.runtime.sendMessage( carting: true, status:
status_color: (item[1]) //item color
);

//more irrelevent carting code

chrome.runtime.sendMessage( carting: true, status:
status_size: (size_text.text) // size
);


this is what I had in the listener page:



chrome.runtime.onMessage.addListener(function(message, sender) 
if (!message.carting) return;

var Status = message.status;

$(function()

$("#tasks_table").append('<tr>'
+'<td>'+item+'</td>'
+'<td>'+color+'</td>'
+'<td>'+size+'</td>'
+'</tr>');




)


The problem with it was is that during the carting process when it yet hasn't found the color it would just constantly add undefined into the table so I found a solution to that issue by using functions:



function waitForItem(item) 
if (typeof item !== "undefined")
$("#tasks_table").append('<tr>'+'<td>'+item+'</td>');
else
setTimeout(waitForItem, 10);



function waitForColor(color)
if (typeof color !== "undefined")
$("#tasks_table").append('<td>'+color+'</td>');
else
setTimeout(waitForColor, 10);



function waitForSize(size)
if (typeof size !== "undefined")
$("#tasks_table").append('<td>'+size+'</td>'+'</tr>');
else
setTimeout(waitForSize, 10);


waitForItem(item);
waitForColor(color);
waitForSize(size);


Now this did work it stopped adding the undefined into the table but now the problem is that it runs the first item function until it is completly done and then so on with each function.



When I have two items it adds the item name for the first item into the table then waits until the second item name is found and adds it too then proceeds to add all the color and the sizes. The problem with this is that my background page works in a loop. It finds the first item, then its color, then its size, then it goes onto the next item, its color, and its size.



So when It finds the first item and then the second item it adds all the rest of the color and size right at the same time. Which I don't want.



I want it to add the info into the table the same way the background page runs so the user can see how smoothly and fast it runs.







javascript jquery html google-chrome-extension thread-synchronization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 7:42









CertainPerformance

69.4k143453




69.4k143453










asked Nov 10 at 7:41









developer12

1239




1239











  • I don't see the need for messaging at all (I think you could do everything right in the content script), but if you want to sequentially execute asynchronous functions such as chrome API methods, you must invoke the next one from a callback of a preceding one and so on, or promisify the API (Using Mozilla WebExtension polyfill for example) and chain via .then() or async/await keywords.
    – wOxxOm
    Nov 10 at 8:20










  • @wOxxOm How would I do it inside the background.js script? because Ive tried it and couldn't figure out how.
    – developer12
    Nov 10 at 8:36










  • "It" - what? I don't even know why you need a background script for this task. As for asynchronous JavaScript (Promise, async/await, etc) you can find a lot of tutorials.
    – wOxxOm
    Nov 10 at 8:49










  • no the background script does all the carting and I send the messages from it to the html loading page can I insert the text into the html page without sending messages? @wOxxOm
    – developer12
    Nov 10 at 9:15










  • Usually you can do it directly.
    – wOxxOm
    Nov 10 at 9:39
















  • I don't see the need for messaging at all (I think you could do everything right in the content script), but if you want to sequentially execute asynchronous functions such as chrome API methods, you must invoke the next one from a callback of a preceding one and so on, or promisify the API (Using Mozilla WebExtension polyfill for example) and chain via .then() or async/await keywords.
    – wOxxOm
    Nov 10 at 8:20










  • @wOxxOm How would I do it inside the background.js script? because Ive tried it and couldn't figure out how.
    – developer12
    Nov 10 at 8:36










  • "It" - what? I don't even know why you need a background script for this task. As for asynchronous JavaScript (Promise, async/await, etc) you can find a lot of tutorials.
    – wOxxOm
    Nov 10 at 8:49










  • no the background script does all the carting and I send the messages from it to the html loading page can I insert the text into the html page without sending messages? @wOxxOm
    – developer12
    Nov 10 at 9:15










  • Usually you can do it directly.
    – wOxxOm
    Nov 10 at 9:39















I don't see the need for messaging at all (I think you could do everything right in the content script), but if you want to sequentially execute asynchronous functions such as chrome API methods, you must invoke the next one from a callback of a preceding one and so on, or promisify the API (Using Mozilla WebExtension polyfill for example) and chain via .then() or async/await keywords.
– wOxxOm
Nov 10 at 8:20




I don't see the need for messaging at all (I think you could do everything right in the content script), but if you want to sequentially execute asynchronous functions such as chrome API methods, you must invoke the next one from a callback of a preceding one and so on, or promisify the API (Using Mozilla WebExtension polyfill for example) and chain via .then() or async/await keywords.
– wOxxOm
Nov 10 at 8:20












@wOxxOm How would I do it inside the background.js script? because Ive tried it and couldn't figure out how.
– developer12
Nov 10 at 8:36




@wOxxOm How would I do it inside the background.js script? because Ive tried it and couldn't figure out how.
– developer12
Nov 10 at 8:36












"It" - what? I don't even know why you need a background script for this task. As for asynchronous JavaScript (Promise, async/await, etc) you can find a lot of tutorials.
– wOxxOm
Nov 10 at 8:49




"It" - what? I don't even know why you need a background script for this task. As for asynchronous JavaScript (Promise, async/await, etc) you can find a lot of tutorials.
– wOxxOm
Nov 10 at 8:49












no the background script does all the carting and I send the messages from it to the html loading page can I insert the text into the html page without sending messages? @wOxxOm
– developer12
Nov 10 at 9:15




no the background script does all the carting and I send the messages from it to the html loading page can I insert the text into the html page without sending messages? @wOxxOm
– developer12
Nov 10 at 9:15












Usually you can do it directly.
– wOxxOm
Nov 10 at 9:39




Usually you can do it directly.
– wOxxOm
Nov 10 at 9:39

















active

oldest

votes











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',
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%2f53236978%2fdont-wait-until-function-finishes-to-run-another-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53236978%2fdont-wait-until-function-finishes-to-run-another-function%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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo