Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

Other ways to replace a table row? – JavaScript – SitePoint Forums

learningcode_x1mckf by learningcode_x1mckf
September 9, 2022
in JavaScript
0
Time limit for notify – JavaScript – SitePoint Forums
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Pay What You Want for this Learn to Code JavaScript Certification Bundle

How to have a Smooth/Fast scroll in mobile popup window? – JavaScript – SitePoint Forums

JavaScript Token (JS) Do the Risks Outweigh the Rewards Wednesday?

fetch() will not be at all times dependable AFAIK as a result of it’s async. I’ve tried to edit and exchange a desk row in a dependable manner for some days and located this strategy to exchange a desk row. Possibly dependable?

  1. Edit document
  2. fetch the document with delay
  3. replace the desk
async perform update_sql(url, json) 
  let physique = get_updatebody(json)
  let obj = JSON.parse(json);
  let id = obj[mod_id]
  await fetch(url, physique)
    .then(response => (response.json())) //no return values
    .then(get_newrow(id));
  end()
  tippy2()


perform get_newrow(id) 
  setTimeout(perform() 
    let url = "https://api3.go4webdev.org/" + mod + "/id/" + id;
    fetch(url)
      .then(response => response.json())
      .then(newdata => replace_tablerow(newdata))
  , 250);


perform replace_tablerow(information) 
 alert(JSON.stringify(information))
 let row = doc.querySelector('desk tr[data-id="' + data[mod_id] + '"]');
 for (let key in information) 
    let val = information[key];
    let cell = row.querySelector('td[data-key="' + key + '"]');
    if (cell) cell.innerHTML = val;
  

Theoretically I can edit and ship return values, however this solely appears to end in object.Promise reply. I interpret that this isn’t dependable in any respect…

  1. Edit document and ship return values
  2. Change the desk row with returned values

Questions:

  1. Is there a much more dependable strategy to edit a document and replace the desk row with new values?
  2. Is there a strategy to learn return values in Javascript and replace a desk row?
  3. Is there another methods to do that which are even easier?

I completely don’t perceive what you need to do?

What means “Edit information”? Who’s enhancing information? The place is it edited?

Why ought to fetch not being dependable solely as a result of its async?

A timeout isn’t ever a good suggestion in controlling asynchronous duties.

Thallius:

I completely don’t perceive what you need to do?

He has give you {a partially} appropriate resolution to the issue, and is asking us for recommendation about the way it may be carried out extra suitably with out the hack of utilizing the delay.

1 Like

sibertius:

fetch() will not be at all times dependable AFAIK as a result of it’s async.

#1 Undecided how that follows. Asynchronous vs synchronous doesn’t make one thing roughly dependable; it’s an online connection from the browser to the online host. If the online host isnt dependable, thats not fetch’s fault.

#2 You’re awaiting the primary fetch, so its not asynchronous anymore. You’re forcing it to be synchronous. Why you’re not awaiting the newrow fetch, i dont know, however you’ve chosen to make it asynchronous.

sibertius:

let url = "https://api3.go4webdev.org/" + mod + "/id/" + id;

mod is undefined, however i’ll assume you’ve outlined it elsewhere, in any other case javascript could be screaming at you within the console. Why you utilize mod right here, however the mod_id from the JSON in every single place else, i’m unsure. Are you certain this isnt your downside? Are you inflicting a race situation someplace to set the worth of mod?

Thallius:

I completely don’t perceive what you need to do?

Choose a row, edit document in database and replace chosen row.

What means “Edit information”? Who’s enhancing information? The place is it edited?

Edit, replace or modify a document in a database.

Why ought to fetch not being dependable solely as a result of its async?

Fetch() is executed, however you can not count on the outcome to be prepared to make use of. It’s AFIAK only a “promise” to ship some information. And generally fetch doesn’t at all times retains the promise because it takes longer time and so on.

A timeout isn’t ever a good suggestion in controlling asynchronous duties.

You hit the nail. That’s the reason I’m asking :slight_smile:

m_hutley:

#1 Asynchronous vs synchronous doesn’t make one thing roughly dependable; it’s an online connection from the browser to the online host. If the online host isn’t dependable, thats not fetch’s fault.

Fetch() doesn’t ship a “prepared” state again AFAIK. So I can’t test if the document is saved earlier than I exploit it (?). Eradicating the delay it fetches the outdated document. The replace will not be absolutely saved.

#2 You’re awaiting the primary fetch, so its not asynchronous anymore. You’re forcing it to be synchronous. Why you’re not awaiting the newrow fetch, i dont know, however you’ve chosen to make it asynchronous.

The important half is to manage that the document is correctly saved so I can use it to replace the desk row. I have no idea if async await fetching the up to date document makes any distinction(?)

sibertius:

let url = "https://api3.go4webdev.org/" + mod + "/id/" + id;
mod is undefined, however i’ll assume you’ve outlined it elsewhere,
in any other case javascript could be screaming at you within the console.
Why you utilize mod right here, however the mod_id from the JSON in every single place else, i’m unsure. Are you certain this isnt your downside? Are you inflicting a race situation someplace to set the worth of mod?

That is an try and make it generic. mod and mod_id is ready on the web page stage (world) and are used to symbolize usr (desk) and user_id (desk column main key) (or tsk and tsk_id for an additional window).

sibertius:

Fetch() doesn’t ship a “prepared” state again AFAIK. So I can’t test if the document is saved earlier than I exploit it (?). Eradicating the delay it fetches the outdated document. The replace will not be absolutely saved.

then is a “accomplished” state for the request that you just despatched. In case your receiving website can be doing asynchronous duties to retailer the document, then there must be a job-completion API for that job, and the distant server must be serving a job reference identifier so that you can test the standing of the commit; you shouldnt be pulling the document simply on a randomly decided timer.

sibertius:

That is an try and make it generic. mod and mod_id is ready on the web page stage (world) and are used to symbolize usr (desk) and user_id (desk column main key) (or tsk and tsk_id for an additional window).

Then why use information[mod_id] within the replace_tablerow? That’s not referencing your world data, it’s referencing what you bought again from the server request…

m_hutley:

then there must be a job-completion API for that job, and the distant server must be serving a job reference identifier so that you can test the standing of the commit; you shouldnt be pulling the document simply on a randomly decided timer.

Any instance?

Then why use information[mod_id] within the replace_tablerow? That’s not referencing your world data, it’s referencing what you bought again from the server request…

let row = doc.querySelector('desk tr[data-id="' + data[mod_id] + '"]');

means “search and get a reference for this row by utilizing data-id: usr_id=2”. Higher solutions?

I’m nonetheless confused.

someplace in your code you ship am async fetch to retailer the adjustments within the database. This request will return a promise. Within the resolve of the promise you might be certain, that the brand new information is saved. So now you’ll be able to name the replace desk and it’ll, after all, fetch the brand new information. (Even when this isn’t obligatory as a result of you have already got this information ship to the server, so why get the identical information again?)

Not a direct instance, however how it could work usually within the case the place a server was taking asynchronous jobs:

Shopper Sends: “Do that please. Right here is the info for ID 9876.”
Server Sends: “Engaged on it. Job ID 1234.”
(pause)
Shopper Sends: “Checking Job ID 1234.”
Server Sends: “In Course of.”
(pause)
Shopper Sends: “Checking Job ID 1234.”
Server Sends: “Job full.”
(At this level, the consumer is aware of the job is finished on the distant server; what they select to do at that time is as much as them, however in your case, it could seem like one thing like)
Shopper Sends: “Get document 9876”
Server Sends: Document 9876 data.

If that’s what your distant server is doing, then that’s how i might count on the move to go; you ballot till you see the job is finished, after which you’ll be able to act on it. I dont know the service you’re calling with that URL, so I don’t know if that’s what’s taking place. It appears to be what you’re suggesting is going on, as a result of in any other case you wouldnt want a delay in any respect.

m_hutley:

Not a direct instance, however how it could work usually within the case the place a server was taking asynchronous jobs:

I See no benefit on this double asynchronous duties. If the consumer will not be ready for the server to complete, the server should not return earlier than he has completed. Every little thing else will result in such a really terrible polling construction which I’d attempt to keep away from at all times.

Thallius:

someplace in your code you ship am async fetch to retailer the adjustments within the database. This request will return a promise. Within the resolve of the promise you might be certain, that the brand new information is saved.

However why does it not work with out the delay?

So now you’ll be able to name the replace desk and it’ll, after all, fetch the brand new information. (Even when this isn’t obligatory as a result of you have already got this information ship to the server, so why get the identical information again?)

That could be a excellent query. I’ve no prepared reply to that :slight_smile: Aside from it confirms that the document is saved correctly.

Should you’re operating an API for database storage for a lot of shoppers, you could need to asynchronously retailer jobs reasonably than dealing with every request synchronously and doubtlessly permitting one connection to halt your service for different transmitters.

If I ship the API 5 GB of knowledge, or a fancy question that’s going to eat processing time, you need to have the ability to nonetheless discipline different requests whereas the job chunks away within the background.

m_hutley:

Should you’re operating an API for database storage for a lot of shoppers, you could need to asynchronously retailer jobs reasonably than dealing with every request synchronously and doubtlessly permitting one connection to halt your service for different transmitters.

Undecided which language/framework will not be in a position to deal with a number of requests in parallel with out ending the primary one, however I’d by no means use it :slight_smile:

My recommendation could be to by no means host an API-based information storage resolution then. shrug

m_hutley:

Shopper Sends: “Checking Job ID 1234.”
Server Sends: “In Course of.”

So I interpret that you just counsel a loop that checks for an instance a time stamp is edited and saved on the document?

I counsel that if the server ISNT doing all your requests synchronously, you’re lacking a step within the course of. With out the service’s documentation, I can’t give a direct reply.

If it IS doing the request synchronously, then there wouldnt be a necessity for a delay, you’ll simply ship the replace, and as quickly as it’s carried out, ship the choose (presumably you’re doing this to confirm the distant server has been up to date efficiently; you’ve already bought the info to place into the row, since you’re the one sending it to the distant server).

Possibly you can begin by telling us which backend and framework you’re utilizing to retailer the info?

I exploit Go + Vanilla JS, CSS and HTML (no framework)

task.go4webdev.org

goTask40

goTask40 is created for customers which are aged 40+

The API is 100% Go (no framework)

crud.go4webdev.org

Go REST API

There appears to be no widespread floor for making a REST Api. Utilizing Go isn’t any exception. My aim is to create a maintainable, quick and easy REST Api.

And the database is Postgresql utilizing the in-built JSON features. (A contact of NoSQL)

I’ve no information of GO so I can’t assist a lot right here…

Are you able to present me the way you name the REST api to retailer the edited information in your JS code?



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Pay What You Want for this Learn to Code JavaScript Certification Bundle

by learningcode_x1mckf
February 2, 2023
0
Pay What You Want for this Learn to Code JavaScript Certification Bundle

Deal Neowin Offers · Oct 4, 2021 - Up to date Jan 31, 2023 13:00 EST Jumpstart your profitable profession in coding and programmingRight now's highlighted deal comes...

Read more

How to have a Smooth/Fast scroll in mobile popup window? – JavaScript – SitePoint Forums

by learningcode_x1mckf
February 2, 2023
0
Different server for Google API – JavaScript – SitePoint Forums

Hello Associates,Sorry I need to appropriate the positioning tackle to this: http://dev.harfrooz.com/I searched quite a bit and I came upon that my downside is expounded to iscroll.js File....

Read more

JavaScript Token (JS) Do the Risks Outweigh the Rewards Wednesday?

by learningcode_x1mckf
February 1, 2023
0
JavaScript Token (JS) Do the Risks Outweigh the Rewards Wednesday?

News Home Wednesday, February 01, 2023 07:38 AM | InvestorsObserver Analysts JavaScript Token receives a excessive risk score from InvestorsObserver evaluation. The proprietary scoring system analyzes how a...

Read more

Discord Rich Presence – JavaScript – SitePoint Forums

by learningcode_x1mckf
February 1, 2023
0
Different server for Google API – JavaScript – SitePoint Forums

Hiya! Extraordinarily new to java-script and I’m making an attempt to make use of discordjs-rpc to make one thing that can change my standing based mostly on no...

Read more

WebAssembly vs. JavaScript: Security, Speed, Flexibility

by learningcode_x1mckf
February 1, 2023
0
WebAssembly vs. JavaScript: Security, Speed, Flexibility

In direction of the start of what's popularly referred to as the World Extensive Net, there was JavaScript. JavaScript has been round since 1995 when Brendan Eich created...

Read more
Next Post
Using the Python and Operator – Real Python

Using the Python and Operator – Real Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Java Is Now On The Nintendo 64!

Java Is Now On The Nintendo 64!

January 12, 2023
Java Web Frameworks Software Market Overview with Detailed Analysis, Competitive landscape, Forecast to 2028 – Panama Travel News

Java Web Frameworks Software Market Overview with Detailed Analysis, Competitive landscape, Forecast to 2028 – Panama Travel News

December 12, 2022
Java InfoQ Trends Report – December 2022

Java InfoQ Trends Report – December 2022

December 17, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?